Advertisement
euis_kusesa

JSONParser

Dec 24th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package com.example.asusx441n.examplelistviewdinamis;
  2.  
  3. import android.util.Log;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.client.utils.URLEncodedUtils;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16.  
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.UnsupportedEncodingException;
  22. import java.util.List;
  23.  
  24. public class JSONParser {
  25.     static InputStream is = null;
  26.     static JSONObject jObj = null;
  27.     static String json = "";
  28.  
  29.     // constructor
  30.     public JSONParser() {
  31.  
  32.     }
  33.  
  34.     // function get json from url
  35.     // by making HTTP POST or GET mehtod
  36.     public JSONObject makeHttpRequest(String url, String method,
  37.                                       List<NameValuePair> params) {
  38.  
  39.         // Making HTTP request
  40.         try {
  41.  
  42.             // check for request method
  43.             if(method == "POST"){
  44.                 // request method is POST
  45.                 // defaultHttpClient
  46.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  47.                 HttpPost httpPost = new HttpPost(url);
  48.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  49.  
  50.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  51.                 HttpEntity httpEntity = httpResponse.getEntity();
  52.                 is = httpEntity.getContent();
  53.  
  54.             }else if(method == "GET"){
  55.                 // request method is GET
  56.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  57.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  58.                 url += "?" + paramString;
  59.                 HttpGet httpGet = new HttpGet(url);
  60.  
  61.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  62.                 HttpEntity httpEntity = httpResponse.getEntity();
  63.                 is = httpEntity.getContent();
  64.             }
  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
Advertisement