Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. public JSONArray getJSONFromUrl(String url) {
  2. 22
  3.  
  4. 23
  5. // Making HTTP request
  6. 24
  7. try {
  8. 25
  9. // defaultHttpClient
  10. 26
  11. DefaultHttpClient httpClient = new DefaultHttpClient();
  12. 27
  13. HttpPost httpPost = new HttpPost(url);
  14. 28
  15. HttpResponse httpResponse = httpClient.execute(httpPost);
  16. 29
  17. HttpEntity httpEntity = httpResponse.getEntity();
  18. 30
  19. is = httpEntity.getContent();
  20. 31
  21. } catch (UnsupportedEncodingException e) {
  22. 32
  23. e.printStackTrace();
  24. 33
  25. } catch (ClientProtocolException e) {
  26. 34
  27. e.printStackTrace();
  28. 35
  29. } catch (IOException e) {
  30. 36
  31. e.printStackTrace();
  32. 37
  33. }
  34. 38
  35. try {
  36. 39
  37. BufferedReader reader = new BufferedReader(new InputStreamReader(
  38. 40
  39. is, "iso-8859-1"), 8);
  40. 41
  41. StringBuilder sb = new StringBuilder();
  42. 42
  43. String line = null;
  44. 43
  45. while ((line = reader.readLine()) != null) {
  46. 44
  47. sb.append(line + "n");
  48. 45
  49. }
  50. 46
  51. is.close();
  52. 47
  53. json = sb.toString();
  54. 48
  55. } catch (Exception e) {
  56. 49
  57. Log.e("Buffer Error", "Error converting result " + e.toString());
  58. 50
  59. }
  60. 51
  61. // try parse the string to a JSON object
  62. 52
  63. try {
  64. 53
  65. jObj = new JSONArray(json);
  66. 54
  67. } catch (JSONException e) {
  68. 55
  69. Log.e("JSON Parser", "Error parsing data " + e.toString());
  70. 56
  71. }
  72. 57
  73. // return JSON String
  74. 58
  75. return jObj;
  76. 59
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement