Android 下使用 JSON 实现 HTTP 请求

不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作
在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便
以下就是一个标准的 JSON 请求的实现过程:
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");  
posted @ 2012-02-01 17:46  与时俱进  阅读(1358)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈