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


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

分類: Android 8499人閱讀 評論(4) 收藏 舉報

不得不說,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對象


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM