使用JSONObject用法

前端之家收集整理的这篇文章主要介绍了使用JSONObject用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package com.jia.networktools;
  2.  
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.methods.HttpGet;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.util.EntityUtils;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11.  
  12. import android.app.Activity;
  13. import android.os.Bundle;
  14. import android.os.Handler;
  15. import android.os.Message;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21.  
  22. import com.google.gson.Gson;
  23.  
  24. public class MainActivity extends Activity implements OnClickListener {
  25. public static final int SHOW_RESPONSE = 0;
  26. private Button sendRequest;
  27. private TextView responseText;
  28. private Handler handler = new Handler() {
  29. public void handleMessage(Message msg) {
  30. switch (msg.what) {
  31. case SHOW_RESPONSE:
  32. String response = (String) msg.obj;
  33. responseText.setText(response);
  34. }
  35. }
  36. };
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. sendRequest = (Button) findViewById(R.id.send_request);
  43. responseText = (TextView) findViewById(R.id.response_text);
  44. sendRequest.setOnClickListener(this);
  45. }
  46.  
  47. @Override
  48. public void onClick(View v) {
  49. if (v.getId() == R.id.send_request) {
  50. sendRequestWithHttpClient();
  51. }
  52.  
  53. }
  54.  
  55. private void sendRequestWithHttpClient() {
  56. new Thread(new Runnable() {
  57. @Override
  58. public void run() {
  59. try {
  60. HttpClient httpClient = new DefaultHttpClient();
  61. HttpGet httpGet = new HttpGet(
  62. "http://192.168.1.88:8080/jia/get_data.json");
  63. HttpResponse httpResponse = httpClient.execute(httpGet);
  64. /*
  65. * HttpPost httpPost=new HttpPost(
  66. * "http://192.168.1.201:8009/interface.PHP?action=get_index_xq"
  67. * ); List<NameValuePair>params=new
  68. * ArrayList<NameValuePair>(); params.add(new
  69. * BasicNameValuePair("Goods_id","1"));
  70. * UrlEncodedFormEntity entity=new
  71. * UrlEncodedFormEntity(params,"utf-8");
  72. * httpPost.setEntity(entity); HttpResponse
  73. * httpResponse=httpClient.execute(httpPost);
  74. */
  75. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  76. // 请求和响应都成功啦
  77. HttpEntity httpentity = httpResponse.getEntity();
  78. String response = EntityUtils.toString(httpentity,"utf-8");
  79.  
  80. Message message = new Message();
  81. message.what = SHOW_RESPONSE;
  82. // 将服务器返回的结果存放到Message中
  83. message.obj = response.toString();
  84. handler.sendMessage(message);
  85. // parseJSONWithGSON(response);
  86. parseJSONWithJSONObject(response);
  87. }
  88. } catch (Exception e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94. private void parseJSONWithGSON(String jsonData) {
  95. Gson gson = new Gson();
  96. // List<App> appList = gson.fromJson(jsonData,new
  97. // TypeToken<List<App>>() {}.getType());
  98. App appList = gson.fromJson(jsonData,App.class);
  99. Log.d("jia","Brand_name is----------->" + appList.getBrand_name());
  100. Log.d("jia","Goods_burden is----------->"
  101. + appList.getGoods_burden());
  102. Log.d("jia","Goods_degree is----------->"
  103. + appList.getGoods_degree());
  104. Log.d("jia","Goods_img is----------->" + appList.getGoods_img());
  105. Log.d("jia","Goods_name is----------->" + appList.getGoods_name());
  106. Log.d("jia","Goods_one_price is----------->"
  107. + appList.getGoods_one_price());
  108. Log.d("jia","Goods_place is----------->" + appList.getGoods_place());
  109. Log.d("jia","Goods_start_weight is----------->"
  110. + appList.getGoods_start_weight());
  111. // for (App app : appList) {
  112. // Log.d("jia","Brand_name is----------->" +
  113. // app.getBrand_name());
  114. // Log.d("jia","Goods_burden is----------->" +
  115. // app.getGoods_burden());
  116. // Log.d("jia","Goods_degree is----------->" +
  117. // app.getGoods_degree());
  118. // Log.d("jia","Goods_img is----------->" +
  119. // app.getGoods_img());
  120. // Log.d("jia","Goods_name is----------->" +
  121. // app.getGoods_name());
  122. // Log.d("jia","Goods_one_price is----------->" +
  123. // app.getGoods_one_price());
  124. // Log.d("jia","Goods_place is----------->" +
  125. // app.getGoods_place());
  126. // Log.d("jia","Goods_start_weight is----------->" +
  127. // app.getGoods_start_weight());
  128. //
  129. // }
  130. }
  131. }).start();
  132. }
  133.  
  134. private void parseJSONWithJSONObject(String jsonData) {
  135. try {
  136. JSONArray jsonArray = new JSONArray(jsonData);
  137. for (int i = 0; i < jsonArray.length(); i++) {
  138. JSONObject jsonObject = jsonArray.getJSONObject(i);
  139. String id = jsonObject.getString("id");
  140. String name = jsonObject.getString("name");
  141. String version = jsonObject.getString("version");
  142. Log.d("jia","id is--------->" + id);
  143. Log.d("jia","name is-------->" + name);
  144. Log.d("jia","version is------->" + version);
  145. }
  146. } catch (Exception e) {
  147. // TODO Auto-generated catch block
  148. e.printStackTrace();
  149. }
  150.  
  151. }
  152. }

猜你在找的Json相关文章