greyhat49

JSONParser

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