Advertisement
Guest User

json class

a guest
Aug 13th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package com.berko.tictactoe;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.content.Context;
  16.  
  17. public class GetJSON {
  18.     Context context = MainActivity.context;
  19.    
  20.     public static JSONObject getJSONfromURL(String url) {
  21.        
  22.         JSONObject jArray = null;
  23.         HttpClient client = new DefaultHttpClient();
  24.         HttpGet request = new HttpGet(url);
  25.        
  26.         String html = "";
  27.        
  28.         InputStream in;
  29.         try {
  30.             HttpResponse response = client.execute(request);
  31.             in = response.getEntity().getContent();
  32.            
  33.             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  34.             StringBuilder str = new StringBuilder();
  35.             String line = null;
  36.             while((line = reader.readLine()) != null)
  37.             {
  38.                 str.append(line);
  39.             }
  40.             in.close();
  41.            
  42.             html = str.toString();
  43.            
  44.         } catch (IllegalStateException e1) {
  45.             // TODO Auto-generated catch block
  46.             e1.printStackTrace();
  47.         } catch (IOException e1) {
  48.             // TODO Auto-generated catch block
  49.             e1.printStackTrace();
  50.         }
  51.        
  52.         try {
  53.             jArray = new JSONObject(html);
  54.         } catch (JSONException e) {
  55.             // TODO Auto-generated catch block
  56.             e.printStackTrace();
  57.         }
  58.        
  59.         return jArray;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement