Android 解析服務器JSON對象

  1. package android.test;  
  2. import java.io.BufferedReader;  
  3. import java.io.InputStreamReader;  
  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.json.JSONArray;  
  9. import org.json.JSONException;  
  10. import org.json.JSONObject;  
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.widget.TextView;  
  15. public class Main extends Activity {  
  16.     private TextView m_textView;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         JSONObject obj = GetJsonObject();  
  23.         StringBuilder sb = new StringBuilder();  
  24.         try {  
  25.             sb.append("product_id: " + obj.getInt("id") + "/n");  
  26.             sb.append("website_name: " + obj.getString("site_name") + "/n");  
  27.             sb.append("title: " + obj.getString("title") + "/n");  
  28.         } catch (JSONException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         m_textView = (TextView) findViewById(R.id.myTextView);  
  32.         m_textView.setText(sb.toString());  
  33.     }  
  34.     private JSONObject GetJsonObject() {  
  35.         HttpClient client = new DefaultHttpClient();  
  36.         StringBuilder builder = new StringBuilder();  
  37.         JSONArray jsonArray = null;  
  38.         HttpGet get = new HttpGet("http://www.test.com/aaa");  
  39.         try {  
  40.             HttpResponse response = client.execute(get);  
  41.             BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
  42.             for (String s = reader.readLine(); s != null; s = reader.readLine()) {  
  43.                 builder.append(s);  
  44.             }  
  45.             Log.i("json_str", builder.toString());  
  46.             jsonArray = new JSONArray(builder.toString());  
  47.             for (int i = 0; i < 2; ++i) {  
  48.                 JSONObject jsonObject = jsonArray.getJSONObject(i);  
  49.                 Log.i("id", jsonObject.getInt("id") + "");  
  50.                 Log.i("website_name", jsonObject.getString("site_name"));  
  51.                 Log.i("website_url", jsonObject.getString("site_url"));  
  52.                 Log.i("category", jsonObject.getInt("category") + "");  
  53.                 Log.i("title", jsonObject.getString("title"));  
  54.             }  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.         try {  
  59.             return jsonArray.getJSONObject(5);  
  60.         } catch (JSONException e) {  
  61.             e.printStackTrace();  
  62.             return null;  
  63.         }  
  64.     }  
  65. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章