Android 下使用 JSON 實現 HTTP 請求,外加幾個示例!

不得不說,JSON 格式的確是非常美妙的,速度快而且簡化了很多操作
在 Android 下,Android SDK 已經爲我們封裝好了整個與 JSON 有關的操作,使用非常方便

以下就是一個標準的 JSON 請求的實現過程:

  1. HttpPost request = new HttpPost(url);
  2. // 先封裝一個 JSON 對象
  3. JSONObject param = new JSONObject();
  4. param.put("name", "rarnu");
  5. param.put("password", "123456");
  6. // 綁定到請求 Entry
  7. StringEntity se = new StringEntity(param.toString());
  8. request.setEntity(se);
  9. // 發送請求
  10. HttpResponse httpResponse = new DefaultHttpClient().execute(request);
  11. // 得到應答的字符串,這也是一個 JSON 格式保存的數據
  12. String retSrc = EntityUtils.toString(httpResponse.getEntity());
  13. // 生成 JSON 對象
  14. JSONObject result = new JSONObject( retSrc);
  15. 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對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章