rachmadi

JSONParser Class

Jan 24th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. package library.danaraddi.com.myapplication;
  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.    
  36.     public JSONObject getJSONFromUrl(final String url) {
  37.  
  38.         // Making HTTP request
  39.         try {
  40.             // Construct the client and the HTTP request.
  41.             DefaultHttpClient httpClient = new DefaultHttpClient();
  42.             HttpPost httpPost = new HttpPost(url);
  43.  
  44.             // Execute the POST request and store the response locally.
  45.             HttpResponse httpResponse = httpClient.execute(httpPost);
  46.             // Extract data from the response.
  47.             HttpEntity httpEntity = httpResponse.getEntity();
  48.             // Open an inputStream with the data content.
  49.             is = httpEntity.getContent();
  50.  
  51.         } catch (UnsupportedEncodingException e) {
  52.             e.printStackTrace();
  53.         } catch (ClientProtocolException e) {
  54.             e.printStackTrace();
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         }
  58.  
  59.         try {
  60.             // Create a BufferedReader to parse through the inputStream.
  61.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  62.                     is, "iso-8859-1"), 8);
  63.             // Declare a string builder to help with the parsing.
  64.             StringBuilder sb = new StringBuilder();
  65.             // Declare a string to store the JSON object data in string form.
  66.             String line = null;
  67.            
  68.             // Build the string until null.
  69.             while ((line = reader.readLine()) != null) {
  70.                 sb.append(line + "\n");
  71.             }
  72.            
  73.             // Close the input stream.
  74.             is.close();
  75.             // Convert the string builder data to an actual string.
  76.             json = sb.toString();
  77.         } catch (Exception e) {
  78.             Log.e("Buffer Error", "Error converting result " + e.toString());
  79.         }
  80.  
  81.         // Try to parse the string to a JSON object
  82.         try {
  83.             jObj = new JSONObject(json);
  84.         } catch (JSONException e) {
  85.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  86.         }
  87.  
  88.         // Return the JSON Object.
  89.         return jObj;
  90.  
  91.     }
  92.    
  93.  
  94.     // function get json from url
  95.     // by making HTTP POST or GET mehtod
  96.     public JSONObject makeHttpRequest(String url, String method,
  97.             List<NameValuePair> params) {
  98.  
  99.         // Making HTTP request
  100.         try {
  101.  
  102.             // check for request method
  103.             if(method == "POST"){
  104.                 // request method is POST
  105.                 // defaultHttpClient
  106.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  107.                 HttpPost httpPost = new HttpPost(url);
  108.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  109.  
  110.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  111.                 HttpEntity httpEntity = httpResponse.getEntity();
  112.                 is = httpEntity.getContent();
  113.  
  114.             }else if(method == "GET"){
  115.                 // request method is GET
  116.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  117.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  118.                 url += "?" + paramString;
  119.                 HttpGet httpGet = new HttpGet(url);
  120.  
  121.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  122.                 HttpEntity httpEntity = httpResponse.getEntity();
  123.                 is = httpEntity.getContent();
  124.             }          
  125.  
  126.         } catch (UnsupportedEncodingException e) {
  127.             e.printStackTrace();
  128.         } catch (ClientProtocolException e) {
  129.             e.printStackTrace();
  130.         } catch (IOException e) {
  131.             e.printStackTrace();
  132.         }
  133.  
  134.         try {
  135.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  136.                     is, "iso-8859-1"), 8);
  137.             StringBuilder sb = new StringBuilder();
  138.             String line = null;
  139.             while ((line = reader.readLine()) != null) {
  140.                 sb.append(line + "\n");
  141.             }
  142.             is.close();
  143.             json = sb.toString();
  144.         } catch (Exception e) {
  145.             Log.e("Buffer Error", "Error converting result " + e.toString());
  146.         }
  147.  
  148.         // try parse the string to a JSON object
  149.         try {
  150.             jObj = new JSONObject(json);
  151.         } catch (JSONException e) {
  152.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  153.         }
  154.  
  155.         // return JSON String
  156.         return jObj;
  157.  
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment