Advertisement
Guest User

Untitled

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