Advertisement
Guest User

JSON Parser

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