Advertisement
Guest User

friday

a guest
Feb 10th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package com.example.cbrown.myapplication;
  2.  
  3.  
  4. import android.os.AsyncTask;
  5. import android.util.Log;
  6.  
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11.  
  12. import java.io.BufferedReader;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15.  
  16. public class MyTask extends AsyncTask<String, Integer, Long> {
  17.  
  18. String jsonString;
  19.  
  20. @Override
  21. protected Long doInBackground(String... strings) {
  22.  
  23. String urlString = strings[0];
  24. DefaultHttpClient httpClient = new DefaultHttpClient();
  25. HttpGet get = new HttpGet(urlString);
  26. try {
  27. HttpResponse response = httpClient.execute(get);
  28. HttpEntity entity = response.getEntity();
  29. InputStream inputStream = entity.getContent();
  30. BufferedReader reader = new BufferedReader(
  31. new InputStreamReader(inputStream));
  32. StringBuilder sb = new StringBuilder();
  33. String line = null;
  34. while ((line = reader.readLine()) != null) {
  35. sb.append(line).append("\n");
  36. }
  37. jsonString = sb.toString();
  38. Log.i("holidays", "jsonString=" + jsonString);
  39.  
  40. } catch (Exception e) {
  41. Log.e("holidays", "HTTPClient exception ", e);
  42. }
  43. return 0L;
  44. }
  45.  
  46. @Override
  47. protected void onPostExecute(Long result) {
  48. Log.i("holidays", "All done, jsonString=" + jsonString);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement