Advertisement
Sergiovan

Untitled

Jan 1st, 2015
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. class JSONfunctions {
  2.     public static JSONArray getJSONfromURL(String url) {
  3.         InputStream is = null;
  4.         String result = "";
  5.         JSONArray jArray = null;
  6.  
  7.         // Download JSON data from URL
  8.         try {
  9.             HttpClient httpclient = new DefaultHttpClient();
  10.             HttpPost httppost = new HttpPost(url);
  11.             HttpResponse response = httpclient.execute(httppost);
  12.             HttpEntity entity = response.getEntity();
  13.             is = entity.getContent();
  14.  
  15.         } catch (Exception e) {
  16.             Log.e("log_tag", "Error in http connection " + e.toString());
  17.         }
  18.  
  19.         // Convert response to string
  20.         try {
  21.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  22.                     is, "iso-8859-1"), 8);
  23.             StringBuilder sb = new StringBuilder();
  24.             String line = null;
  25.             while ((line = reader.readLine()) != null) {
  26.                 sb.append(line + "\n");
  27.             }
  28.             is.close();
  29.             result = sb.toString();
  30.         } catch (Exception e) {
  31.             Log.e("log_tag", "Error converting result " + e.toString());
  32.         }
  33.  
  34.         try {
  35.  
  36.             jArray = new JSONArray(result);
  37.         } catch (JSONException e) {
  38.             Log.e("log_tag", "Error parsing data " + e.toString());
  39.  
  40.         }
  41.  
  42.         return jArray;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement