Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labs.mobile.lab_6_piratecrew;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.Inet4Address;
- import java.net.URL;
- public class DisplayJSONActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_display_json);
- TextView textView = (TextView)findViewById(R.id.JSONTextView);
- textView.setText("Downloading JSON!");
- new downloadJSON().execute("http://www.hull.ac.uk/php/349628/08027/labs/pirates.json");
- }
- private class downloadJSON extends AsyncTask<String,String,String >
- {
- protected String doInBackground(String... args)
- {
- String result = "";
- try
- {
- InputStream stream = (InputStream)new URL(args[0]).getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
- String line = "";
- while(line!=null)
- {
- result += line;
- line = reader.readLine();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return result;
- }
- protected void onPostExecute(String pResult)
- {
- TextView textView = (TextView)findViewById(R.id.JSONTextView);
- textView.setText(pResult);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement