Android 下使用 JSON 实现 HTTP 请求

<p>不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作</p><p>在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便</p><p>以下就是一个标准的 JSON 请求的实现过程:</p><pre class="brush:java;toolbar:false">HttpPostrequest=newHttpPost(url); //先封装一个JSON对象 JSONObjectparam=newJSONObject(); param.put(&quot;name&quot;,&quot;rarnu&quot;); param.put(&quot;password&quot;,&quot;123456&quot;); //绑定到请求Entry StringEntityse=newStringEntity(param.toString()); request.setEntity(se); //发送请求 HttpResponsehttpResponse=newDefaultHttpClient().execute(request); //得到应答的字符串,这也是一个JSON格式保存的数据 StringretSrc=EntityUtils.toString(httpResponse.getEntity()); //生成JSON对象 JSONObjectresult=newJSONObject(retSrc); Stringtoken=result.get(&quot;token&quot;);</pre><p></p><p></p>
RangeTime:0.006786s
RangeMem:204.56 KB
返回顶部 留言