thieumao

JSONParser .java - đọc JSON

Dec 4th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package thieu.mao.xosovui;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import java.util.List;
  9.  
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.NameValuePair;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.client.methods.HttpGet;
  16. import org.apache.http.client.methods.HttpPost;
  17. import org.apache.http.client.utils.URLEncodedUtils;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import android.util.Log;
  23.  
  24. public class JSONParser {
  25.  
  26.     static InputStream is = null;
  27.     static JSONObject jObj = null;
  28.     static String json = "";
  29.  
  30.     // constructor
  31.     public JSONParser() {
  32.  
  33.     }
  34.  
  35.     // function get json from url
  36.     // by making HTTP POST or GET mehtod
  37.     public JSONObject makeHttpRequest(String url, String method,
  38.             List<NameValuePair> params) {
  39.  
  40.         // Making HTTP request
  41.         try {
  42.  
  43.             // check for request method
  44.             if (method == "POST") {
  45.                 // request method is POST
  46.                 // defaultHttpClient
  47.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  48.                 HttpPost httpPost = new HttpPost(url);
  49.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  50.  
  51.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  52.                 HttpEntity httpEntity = httpResponse.getEntity();
  53.                 is = httpEntity.getContent();
  54.  
  55.             } else if (method == "GET") {
  56.                 // request method is GET
  57.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  58.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  59.                 url += "?" + paramString;
  60.                 HttpGet httpGet = new HttpGet(url);
  61.  
  62.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  63.                 HttpEntity httpEntity = httpResponse.getEntity();
  64.                 is = httpEntity.getContent();
  65.             }
  66.  
  67.         } catch (UnsupportedEncodingException e) {
  68.             e.printStackTrace();
  69.         } catch (ClientProtocolException e) {
  70.             e.printStackTrace();
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }
  74.  
  75.         try {
  76.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  77.                     is, "iso-8859-1"), 8);
  78.             StringBuilder sb = new StringBuilder();
  79.             String line = null;
  80.             while ((line = reader.readLine()) != null) {
  81.                 sb.append(line + "\n");
  82.             }
  83.             is.close();
  84.             json = sb.toString();
  85.         } catch (Exception e) {
  86.             Log.e("Buffer Error", "Error converting result " + e.toString());
  87.         }
  88.  
  89.         // try parse the string to a JSON object
  90.         try {
  91.             jObj = new JSONObject(json);
  92.         } catch (JSONException e) {
  93.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  94.         }
  95.  
  96.         // return JSON String
  97.         return jObj;
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment