Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. package labs.mobile.lab_6_piratecrew;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.TextView;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.net.Inet4Address;
  13. import java.net.URL;
  14.  
  15. public class DisplayJSONActivity extends AppCompatActivity {
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_display_json);
  21. TextView textView = (TextView)findViewById(R.id.JSONTextView);
  22. textView.setText("Downloading JSON!");
  23. new downloadJSON().execute("http://www.hull.ac.uk/php/349628/08027/labs/pirates.json");
  24. }
  25.  
  26. private class downloadJSON extends AsyncTask<String,String,String >
  27. {
  28. protected String doInBackground(String... args)
  29. {
  30. String result = "";
  31. try
  32. {
  33. InputStream stream = (InputStream)new URL(args[0]).getContent();
  34. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  35. String line = "";
  36. while(line!=null)
  37. {
  38. result += line;
  39. line = reader.readLine();
  40. }
  41. }
  42. catch (Exception e)
  43. {
  44. e.printStackTrace();
  45. }
  46. return result;
  47. }
  48.  
  49. protected void onPostExecute(String pResult)
  50. {
  51. TextView textView = (TextView)findViewById(R.id.JSONTextView);
  52. textView.setText(pResult);
  53. }
  54.  
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement