Advertisement
Jukik

Untitled

May 30th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.List;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.client.utils.URLEncodedUtils;
  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.     // function get json from url
  34.     // by making HTTP POST or GET mehtod
  35.     public JSONObject makeHttpRequest(String url, String method,
  36.             List<NameValuePair> params) {
  37.  
  38.         // Making HTTP request
  39.         try {
  40.            
  41.             // check for request method
  42.             if(method == "POST"){
  43.                 // request method is POST
  44.                 // defaultHttpClient
  45.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  46.                 HttpPost httpPost = new HttpPost(url);
  47.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  48.  
  49.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  50.                 HttpEntity httpEntity = httpResponse.getEntity();
  51.                 is = httpEntity.getContent();
  52.                
  53.             }else if(method == "GET"){
  54.                 // request method is GET
  55.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  56.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  57.                 url += "?" + paramString;
  58.                 HttpGet httpGet = new HttpGet(url);
  59.  
  60.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  61.                 HttpEntity httpEntity = httpResponse.getEntity();
  62.                 is = httpEntity.getContent();
  63.             }          
  64.            
  65.  
  66.         } catch (UnsupportedEncodingException e) {
  67.             e.printStackTrace();
  68.         } catch (ClientProtocolException e) {
  69.             e.printStackTrace();
  70.         } catch (IOException e) {
  71.             e.printStackTrace();
  72.         }
  73.  
  74.         try {
  75.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  76.                     is, "iso-8859-1"), 8);
  77.             StringBuilder sb = new StringBuilder();
  78.             String line = null;
  79.             while ((line = reader.readLine()) != null) {
  80.                 sb.append(line + "\n");
  81.             }
  82.             is.close();
  83.             json = sb.toString();
  84.         } catch (Exception e) {
  85.             Log.e("Buffer Error", "Error converting result " + e.toString());
  86.         }
  87.  
  88.         // try parse the string to a JSON object
  89.         try {
  90.             jObj = new JSONObject(json);
  91.         } catch (JSONException e) {
  92.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  93.         }
  94.  
  95.         // return JSON String
  96.         return jObj;
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement