thieumao

JSONParser,java - Phim Hay appphim.com

Nov 24th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package com.appphim.xemphim.phimhay.utils;
  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.HttpVersion;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.ClientProtocolException;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.params.BasicHttpParams;
  20. import org.apache.http.params.HttpParams;
  21. import org.apache.http.params.HttpProtocolParams;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. import android.util.Log;
  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.     public JSONObject makeHttpRequest(String url, String method,
  39.             List<NameValuePair> params) {
  40.  
  41.         // Making HTTP request
  42.         try {
  43.  
  44.             // check for request method
  45.             if (method == "POST") {
  46.                 // request method is POST
  47.                 // defaultHttpClient
  48.                 // >> Moi them
  49.                 HttpParams par = new BasicHttpParams();
  50.                 HttpProtocolParams.setVersion(par, HttpVersion.HTTP_1_1);
  51.                 HttpProtocolParams.setContentCharset(par, "utf-8");
  52.                 par.setBooleanParameter("http.protocol.expect-continue", false);
  53.                 // >> moi them
  54.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  55.                 HttpPost httpPost = new HttpPost(url);
  56.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  57.  
  58.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  59.                 HttpEntity httpEntity = httpResponse.getEntity();
  60.                 is = httpEntity.getContent();
  61.  
  62.             } else if (method == "GET") {
  63.                 // request method is GET
  64.                 // >> Moi them
  65.                 HttpParams par = new BasicHttpParams();
  66.                 HttpProtocolParams.setVersion(par, HttpVersion.HTTP_1_1);
  67.                 HttpProtocolParams.setContentCharset(par, "utf-8");
  68.                 par.setBooleanParameter("http.protocol.expect-continue", false);
  69.                 // >> moi them
  70.                 DefaultHttpClient httpClient = new DefaultHttpClient(par);
  71.                 //String paramString = URLEncodedUtils.format(params, "utf-8");
  72. //              url += "?" + paramString;
  73.                 HttpGet httpGet = new HttpGet(url);
  74.  
  75.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  76.                 HttpEntity httpEntity = httpResponse.getEntity();
  77.                 is = httpEntity.getContent();
  78.             }
  79.  
  80.         } catch (UnsupportedEncodingException e) {
  81.             e.printStackTrace();
  82.         } catch (ClientProtocolException e) {
  83.             e.printStackTrace();
  84.         } catch (IOException e) {
  85.             e.printStackTrace();
  86.         }
  87.  
  88.         try {
  89. //          BufferedReader reader = new BufferedReader(new InputStreamReader(
  90. //                  is, "iso-8859-1"), 8);
  91.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  92.                     is, "utf-8"), 8);
  93.             StringBuilder sb = new StringBuilder();
  94.             String line = null;
  95.             while ((line = reader.readLine()) != null) {
  96.                 sb.append(line + "\n");
  97.             }
  98.             is.close();
  99.             json = sb.toString();
  100.         } catch (Exception e) {
  101.             Log.e("Buffer Error", "Error converting result " + e.toString());
  102.         }
  103.  
  104.         // try parse the string to a JSON object
  105.         try {
  106.             jObj = new JSONObject(json);
  107.         } catch (JSONException e) {
  108.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  109.         }
  110.  
  111.         // return JSON String
  112.         return jObj;
  113.  
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment