Android 下使用 JSON 實現 HTTP 要求,外加幾個樣本!

來源:互聯網
上載者:User

標籤:

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

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

 

[java] view plaincopyprint? 
  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對象

Android 下使用 JSON 實現 HTTP 要求,外加幾個樣本!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.