Advertisement
vipeltaja89

Get Json from URL

Sep 24th, 2013
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. //--- REMEMBER TO ADD PERMISSION (android.permission.INTERNET) TO ANDROID MANIFEST !!! ---//
  2.  
  3. // Background task class
  4. public class HttpGetTask extends AsyncTask<String, Void, String> {
  5.    
  6.     interface OnHttpGetListener {
  7.         public void httpGetCompleted(String response);
  8.         public void httpGetFailed(String error);
  9.     }
  10.  
  11.     private OnHttpGetListener mListener;
  12.     private boolean mGetFailed;
  13.  
  14.     public HttpGetTask(OnHttpGetListener listener) {
  15.         mListener = listener;
  16.     }
  17.    
  18.     @Override
  19.     protected String doInBackground(String... params) {
  20.         try {
  21.             HttpGet uri = new HttpGet(params[0]);  
  22.             DefaultHttpClient client = new DefaultHttpClient();
  23.        
  24.             HttpResponse resp = client.execute(uri);
  25.             StatusLine status = resp.getStatusLine();
  26.             if( status.getStatusCode() == 200 ) {
  27.                 ByteArrayOutputStream out = new ByteArrayOutputStream();
  28.                         resp.getEntity().writeTo(out);
  29.                         out.close();
  30.                         return out.toString();
  31.             } else {
  32.                 throw new IllegalArgumentException("Status code was not 200");
  33.             }
  34.         }
  35.         catch (Exception e) {
  36.             mGetFailed = true;
  37.             return e.getMessage();
  38.         }      
  39.     }
  40.    
  41.     @Override
  42.     protected void onPostExecute(String response) {
  43.         if(mGetFailed) {
  44.             mListener.httpGetFailed(response);
  45.         } else {
  46.             mListener.httpGetCompleted(response);
  47.         }
  48.     }
  49. }
  50.  
  51. //-----------------------------------------------------------------//
  52. // Exaple how to use that
  53. public class MainActivity extends Activity implements OnHttpGetListener {
  54.  
  55.     public static final String TAG = "myApp";
  56.  
  57.     @Override
  58.     protected void onCreate(Bundle savedInstanceState) {
  59.         super.onCreate(savedInstanceState);
  60.         setContentView(R.layout.activity_main);
  61.         getJSON();
  62.     }
  63.  
  64.     private void getJSON() {
  65.         HttpGetTask jsonGetter = new HttpGetTask(this); // Registers MainActivity to listen callbacks
  66.         final String jsonUrl = // url to json
  67.         jsonGetter.execute(jsonUrl); // Starts the async task
  68.     }
  69.  
  70.     // Callbacks from the async task
  71.     @Override
  72.     public void httpGetCompleted(String response) {
  73.         // This gets called when the background task
  74.         // to get json is completed without errors
  75.         Toast.makeText(this, "Response: " + response, Toast.LENGTH_SHORT).show();
  76.         Log.d(TAG, response); // printing to debug
  77.     }
  78.  
  79.     @Override
  80.     public void httpGetFailed(String error) {
  81.         // This gets called when the background task failes to get json
  82.         Toast.makeText(this, "Http get failed!", Toast.LENGTH_SHORT).show();
  83.         Log.e(TAG, error); // printing to error
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement