Advertisement
radityakurnianto

JSONParser.java

May 19th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.radit.mysql;
  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;
  27.     static JSONObject jObj = null;
  28.     static String json = "";
  29.    
  30.     public JSONParser()
  31.     {
  32.        
  33.     }
  34.    
  35.     public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params)
  36.     {
  37.         try
  38.         {
  39.             if(method == "POST")
  40.             {
  41.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  42.                 HttpPost httpPost = new HttpPost(url);
  43.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  44.                
  45.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  46.                 HttpEntity httpEntity = httpResponse.getEntity();
  47.                 is = httpEntity.getContent();
  48.             }
  49.             else if(method == "GET")
  50.             {
  51.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  52.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  53.                 url += "?" + paramString;
  54.                 HttpGet httpGet = new HttpGet(url);
  55.  
  56.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  57.                 HttpEntity httpEntity = httpResponse.getEntity();
  58.                 is = httpEntity.getContent();
  59.             }
  60.         }
  61.         catch (UnsupportedEncodingException e)
  62.         {
  63.             e.printStackTrace();
  64.         }
  65.         catch (ClientProtocolException e)
  66.         {
  67.             e.printStackTrace();
  68.         }
  69.         catch (IOException e)
  70.         {
  71.             e.printStackTrace();
  72.         }
  73.        
  74.         try {
  75.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  76.                     is, "ISO-8859-1"), 8);
  77.             StringBuilder sb = new StringBuilder();
  78.             String line = null;
  79.             while ((line = reader.readLine()) != null) {
  80.                 sb.append(line + "\n");
  81.             }
  82.             is.close();
  83.             json = sb.toString();
  84.         }
  85.         catch (Exception e)
  86.         {
  87.             Log.e("Buffer Error", "Error converting result " + e.toString());
  88.         }
  89.  
  90.         // try parse the string to a JSON object
  91.         try
  92.         {
  93.             jObj = new JSONObject(json);
  94.         }
  95.         catch (JSONException e)
  96.         {
  97.             Log.e("JSON Parser", "Error parsing data " + e.toString() + "" + json);
  98.         }
  99.  
  100.         // return JSON String
  101.         return jObj;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement