atultiwari

JASONParser.java

Mar 6th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package in.medicalguru.settings;
  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.HttpPost;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import android.util.Log;
  21.  
  22. public class JSONParser {
  23.  
  24.     static InputStream is = null;
  25.     static JSONObject jObj = null;
  26.     static String json = "";
  27.  
  28.     // constructor
  29.     public JSONParser() {
  30.  
  31.     }
  32.    
  33.     public JSONObject getJSONFromUrl(String url) {
  34.  
  35.         // Making HTTP request
  36.         try {
  37.             // defaultHttpClient
  38.             DefaultHttpClient httpClient = new DefaultHttpClient();
  39.             HttpPost httpPost = new HttpPost(url);
  40.  
  41.             HttpResponse httpResponse = httpClient.execute(httpPost);
  42.             HttpEntity httpEntity = httpResponse.getEntity();
  43.             is = httpEntity.getContent();          
  44.  
  45.         } catch (UnsupportedEncodingException e) {
  46.             e.printStackTrace();
  47.         } catch (ClientProtocolException e) {
  48.             e.printStackTrace();
  49.         } catch (IOException e) {
  50.             e.printStackTrace();
  51.         }
  52.        
  53.         try {
  54.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  55.                     is, "iso-8859-1"), 8);
  56.             StringBuilder sb = new StringBuilder();
  57.             String line = null;
  58.             while ((line = reader.readLine()) != null) {
  59.                 sb.append(line + "\n");
  60.             }
  61.             is.close();
  62.             json = sb.toString();
  63.         } catch (Exception e) {
  64.             Log.e("Buffer Error", "Error converting result " + e.toString());
  65.         }
  66.  
  67.         // try parse the string to a JSON object
  68.         try {
  69.             jObj = new JSONObject(json);
  70.         } catch (JSONException e) {
  71.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  72.         }
  73.  
  74.         // return JSON String
  75.         return jObj;
  76.     }
  77.    
  78.     public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {
  79.  
  80.         // Making HTTP request
  81.         try {
  82.             // defaultHttpClient
  83.             DefaultHttpClient httpClient = new DefaultHttpClient();
  84.             HttpPost httpPost = new HttpPost(url);
  85.             if (params != null) {
  86.             httpPost.setEntity(new UrlEncodedFormEntity(params));
  87.             }
  88.             HttpResponse httpResponse = httpClient.execute(httpPost);
  89.             HttpEntity httpEntity = httpResponse.getEntity();
  90.             is = httpEntity.getContent();          
  91.  
  92.         } catch (UnsupportedEncodingException e) {
  93.             e.printStackTrace();
  94.         } catch (ClientProtocolException e) {
  95.             e.printStackTrace();
  96.         } catch (IOException e) {
  97.             e.printStackTrace();
  98.         }
  99.        
  100.         try {
  101.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  102.                     is, "iso-8859-1"), 8);
  103.             StringBuilder sb = new StringBuilder();
  104.             String line = null;
  105.             while ((line = reader.readLine()) != null) {
  106.                 sb.append(line + "\n");
  107.             }
  108.             is.close();
  109.             json = sb.toString();
  110.         } catch (Exception e) {
  111. //          Log.e("Buffer Error", "Error converting result " + e.toString());
  112.         }
  113.  
  114.         // try parse the string to a JSON object
  115.         try {
  116.             jObj = new JSONObject(json);
  117.         } catch (JSONException e) {
  118.     //      Log.e("JSON Parser", "Error parsing data " + e.toString());
  119.         }
  120.  
  121.         // return JSON String
  122.         return jObj;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment