Advertisement
Guest User

Untitled

a guest
May 31st, 2014
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package com.example.staggeredgridviewdemo;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6.  
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.util.Log;
  16.  
  17. public class JSONfunctions {
  18.  
  19. public static JSONObject getJSONfromURL(String url) {
  20. InputStream is = null;
  21. String result = "";
  22. JSONObject jArray = null;
  23.  
  24. // Download JSON data from URL
  25. try {
  26. HttpClient httpclient = new DefaultHttpClient();
  27. HttpPost httppost = new HttpPost(url);
  28. HttpResponse response = httpclient.execute(httppost);
  29. HttpEntity entity = response.getEntity();
  30. is = entity.getContent();
  31.  
  32. } catch (Exception e) {
  33. Log.e("log_tag", "Error in http connection " + e.toString());
  34. }
  35.  
  36. // Convert response to string
  37. try {
  38. BufferedReader reader = new BufferedReader(new InputStreamReader(
  39. is, "iso-8859-1"), 8);
  40. StringBuilder sb = new StringBuilder();
  41. String line = null;
  42. while ((line = reader.readLine()) != null) {
  43. sb.append(line + "\n");
  44. }
  45. is.close();
  46. result = sb.toString();
  47. } catch (Exception e) {
  48. Log.e("log_tag", "Error converting result " + e.toString());
  49. }
  50.  
  51. try {
  52.  
  53. jArray = new JSONObject(result);
  54. } catch (JSONException e) {
  55. Log.e("log_tag", "Error parsing data " + e.toString());
  56. }
  57.  
  58. return jArray;
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement