thieumao

JSONParser UTF8

Jul 1st, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. package com.cudo.vlog.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.client.utils.URLEncodedUtils;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.params.BasicHttpParams;
  21. import org.apache.http.params.HttpParams;
  22. import org.apache.http.params.HttpProtocolParams;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import android.util.Log;
  27.  
  28. public class JSONParser {
  29.  
  30.     static InputStream is = null;
  31.     static JSONObject jObj = null;
  32.     static String json = "";
  33.  
  34.     // constructor
  35.     public JSONParser() {
  36.  
  37.     }
  38.  
  39.     // function get json from url
  40.     // by making HTTP POST or GET mehtod
  41.     public JSONObject makeHttpRequest(String url, String method,
  42.             List<NameValuePair> params) {
  43.  
  44.         // Making HTTP request
  45.         try {
  46.  
  47.             // check for request method
  48.             if (method == "POST") {
  49.                 // request method is POST
  50.                 // defaultHttpClient
  51.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  52.                 HttpPost httpPost = new HttpPost(url);
  53.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  54.  
  55.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  56.                 HttpEntity httpEntity = httpResponse.getEntity();
  57.                 is = httpEntity.getContent();
  58.  
  59.             } else if (method == "GET") {
  60.                 // request method is GET
  61.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  62.                 String paramString = URLEncodedUtils.format(params, "utf-8");
  63.                 url += "?" + paramString;
  64.                 HttpGet httpGet = new HttpGet(url);
  65.  
  66.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  67.                 HttpEntity httpEntity = httpResponse.getEntity();
  68.                 is = httpEntity.getContent();
  69.             }
  70.  
  71.         } catch (UnsupportedEncodingException e) {
  72.             e.printStackTrace();
  73.         } catch (ClientProtocolException e) {
  74.             e.printStackTrace();
  75.         } catch (IOException e) {
  76.             e.printStackTrace();
  77.         }
  78.  
  79.         try {
  80.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  81.                     is, "iso-8859-1"), 8);
  82.             StringBuilder sb = new StringBuilder();
  83.             String line = null;
  84.             while ((line = reader.readLine()) != null) {
  85.                 sb.append(line + "\n");
  86.             }
  87.             is.close();
  88.             json = sb.toString();
  89.         } catch (Exception e) {
  90.             Log.e("Buffer Error", "Error converting result " + e.toString());
  91.         }
  92.  
  93.         // try parse the string to a JSON object
  94.         try {
  95.             jObj = new JSONObject(json);
  96.         } catch (JSONException e) {
  97.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  98.         }
  99.  
  100.         // return JSON String
  101.         return jObj;
  102.  
  103.     }
  104.  
  105.     public JSONObject makeHttpRequest2(String url, String method,
  106.             List<NameValuePair> params) {
  107.  
  108.         // Making HTTP request
  109.         try {
  110.  
  111.             // check for request method
  112.             if (method == "POST") {
  113.                 // request method is POST
  114.                 // defaultHttpClient
  115.                 // >> Moi them
  116.                 HttpParams par = new BasicHttpParams();
  117.                 HttpProtocolParams.setVersion(par, HttpVersion.HTTP_1_1);
  118.                 HttpProtocolParams.setContentCharset(par, "utf-8");
  119.                 par.setBooleanParameter("http.protocol.expect-continue", false);
  120.                 // >> moi them
  121.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  122.                 HttpPost httpPost = new HttpPost(url);
  123.                 httpPost.setEntity(new UrlEncodedFormEntity(params));
  124.  
  125.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  126.                 HttpEntity httpEntity = httpResponse.getEntity();
  127.                 is = httpEntity.getContent();
  128.  
  129.             } else if (method == "GET") {
  130.                 // request method is GET
  131.                 // >> Moi them
  132.                 HttpParams par = new BasicHttpParams();
  133.                 HttpProtocolParams.setVersion(par, HttpVersion.HTTP_1_1);
  134.                 HttpProtocolParams.setContentCharset(par, "utf-8");
  135.                 par.setBooleanParameter("http.protocol.expect-continue", false);
  136.                 // >> moi them
  137.                 DefaultHttpClient httpClient = new DefaultHttpClient(par);
  138.                 //String paramString = URLEncodedUtils.format(params, "utf-8");
  139. //              url += "?" + paramString;
  140.                 HttpGet httpGet = new HttpGet(url);
  141.  
  142.                 HttpResponse httpResponse = httpClient.execute(httpGet);
  143.                 HttpEntity httpEntity = httpResponse.getEntity();
  144.                 is = httpEntity.getContent();
  145.             }
  146.  
  147.         } catch (UnsupportedEncodingException e) {
  148.             e.printStackTrace();
  149.         } catch (ClientProtocolException e) {
  150.             e.printStackTrace();
  151.         } catch (IOException e) {
  152.             e.printStackTrace();
  153.         }
  154.  
  155.         try {
  156. //          BufferedReader reader = new BufferedReader(new InputStreamReader(
  157. //                  is, "iso-8859-1"), 8);
  158.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  159.                     is, "utf-8"), 8);
  160.             StringBuilder sb = new StringBuilder();
  161.             String line = null;
  162.             while ((line = reader.readLine()) != null) {
  163.                 sb.append(line + "\n");
  164.             }
  165.             is.close();
  166.             json = sb.toString();
  167.         } catch (Exception e) {
  168.             Log.e("Buffer Error", "Error converting result " + e.toString());
  169.         }
  170.  
  171.         // try parse the string to a JSON object
  172.         try {
  173.             jObj = new JSONObject(json);
  174.         } catch (JSONException e) {
  175.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  176.         }
  177.  
  178.         // return JSON String
  179.         return jObj;
  180.  
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment