Advertisement
Ellie29

Untitled

Dec 10th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. public class JSONParser {
  2.     //variables
  3.     static InputStream in = null;
  4.     static JSONObject jObject = null;
  5.     static String jsonString = "";
  6.     // constructor
  7.     public JSONParser() { }
  8.  
  9.     // function get json from url by making HTTP request POST or GET
  10.     public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
  11.  
  12.         // creating HTTP request
  13.         try {
  14.             // check for type of requested method
  15.             if(method == "POST"){
  16.                 // requested method - POST
  17.                 DefaultHttpClient client = new DefaultHttpClient();
  18.                 HttpPost post = new HttpPost(url);
  19.                 post.setEntity(new UrlEncodedFormEntity(params));
  20.  
  21.                 HttpResponse response = client.execute(post);
  22.                 HttpEntity entity = response.getEntity();
  23.                 in = entity.getContent();
  24.  
  25.             }else if(method == "GET"){
  26.                 // requested method i- GET
  27.                 DefaultHttpClient client = new DefaultHttpClient();
  28.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  29.                 url += "?" + paramString;
  30.                 HttpGet get = new HttpGet(url);
  31.  
  32.                 HttpResponse httpResponse = client.execute(get);
  33.                 HttpEntity entity = httpResponse.getEntity();
  34.                 in = entity.getContent();
  35.             }
  36.  
  37.         } catch (UnsupportedEncodingException e) {
  38.             e.printStackTrace();
  39.         } catch (ClientProtocolException e) {
  40.             e.printStackTrace();
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }
  44.  
  45.         try {
  46.             //reading data
  47.             BufferedReader readerBuffer = new BufferedReader(new InputStreamRea-der(in, "iso-8859-1"), 8);
  48.             StringBuilder stringBuild = new StringBuilder();
  49.             String line = null;
  50.             while ((line = readerBuffer.readLine()) != null) {
  51.                 stringBuild.append(line + "\n");
  52.             }
  53.             in.close();
  54.             jsonString = stringBuild.toString();
  55.         } catch (Exception e) {
  56.             Log.e("Buffer Error", "Error while converting" + e.toString());
  57.         }
  58.  
  59.         // parsing the string to a JSON object
  60.         try {
  61.             jObject = new JSONObject(jsonString);
  62.         } catch (JSONException e) {
  63.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  64.         }
  65.         // return JSON Object
  66.         return jObject;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement