Android 下使用 JSON 实现 HTTP 请求

不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作
在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便

以下就是一个标准的 JSON 请求的实现过程:

[java] view plaincopyprint?

    HttpPost request = new HttpPost(url);  
    // 先封装一个 JSON 对象  
    JSONObject param = new JSONObject();  
    param.put("name", "rarnu");  
    param.put("password", "123456");  
    // 绑定到请求 Entry  
    StringEntity se = new StringEntity(param.toString());   
    request.setEntity(se);  
    // 发送请求  
    HttpResponse httpResponse = new DefaultHttpClient().execute(request);  
    // 得到应答的字符串,这也是一个 JSON 格式保存的数据  
    String retSrc = EntityUtils.toString(httpResponse.getEntity());  
    // 生成 JSON 对象  
    JSONObject result = new JSONObject( retSrc);  
    String token = result.get("token");  

android下支持JSON的远程访问(推荐此BLOG):http://marshal.easymorse.com/archives/1707

关于android JSON写入类--JsonWriter,轻松生成JSON格式的数据:http://disanji.net/2011/03/05/android-3-0-json-jsonwriter/

android JSON解析示例代码,每日维基widget:http://www.android123.com.cn/androidkaifa/664.html

客户端向服务器端发送数据,一种是在url中带参数,一种是json数据发送方式(小陌):http://henzil.easymorse.com/?p=241

android访问PHP取回JSON数据:http://blog.lrenwang.com/post/114/

如何用android JSON对象发送一个请求:

http://cn.webdiscussion.info/question/3027066/如何发送一个请求在与Android-JSON对象

posted @ 2013-12-25 12:27  涵野  阅读(512)  评论(0编辑  收藏  举报