This is an old revision of the document!


05 - Android Connectivity & Google APIs

  • Description:
  • Practical part:

Lecture

Practical

Resources

Task 1 - Downloading remote content

Create a new project with a blank main Activity. Add an EditText, a Button and a TextView to the Activity's layout. When the user enters a URL into the EditText and clicks the Button download the remote HTTP content at that URL and display it into the TextView.

By design, the Android framework will not allow long-running or potentially blocking operations (such as network operations) to be carried out on the main UI thread. As such, within the onClick() method of the Button we want to start a new thread in which to download the HTTP data.

The simplest solution would be using a Thread instance. However, we want to modify a UI element (the TextView) right after the thread has finished downloading data, which cannot be done from a Thread's run() method. For such a scenario, the framework provides the AsyncTask class.

When defining the AsyncTask instance we can provide 3 parameters. Since we want to give our instance a String (the URL) to work with and have it return another String (the downloaded content) the first and third parameters will be String while the second one can be left Void:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v){
        AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>(){
        ...
        };
    }
});

The most important method within the AsyncTask is doInBackground(), which is executed on another thread. Since we have defined the AsyncTask as having a String as the first and third parameters, the signature of the doInBackground() method will be:

@Override
protected String doInBackground(String... params){
...
}

In order to retrieve HTTP data, we can use the HttpURLConnection API, using params[0] as the remote URL:

URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

The actual contents can be retrieved by using the InputStream provided by the HttpURLConnection instance:

InputStream is = connection.getInputStream();

The String contents returned from the doInBackground() method are passed as the parameter to the onPostExecute() method, which will be executed on the main UI thread. As such, within this method we can safely update the contents of the TextView:

@Override
protected void onPostExecute(String content) {
  textView.setText(content);
}

Having defined the AsyncTask launch it using the execute method, which receives the text from the EditText element as a parameter:

task.execute(editText.getText().toString());

If you try to run the application at this time, it will stop since, by default, applications are not granted Internet access by the framework. In order to be able to connect to remote resources, you need to add the necessary permission to the AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
osp/lectures/lecture-connectivity.1477903223.txt.gz ยท Last modified: 2016/10/31 10:40 by vlad.traista
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0