Developing app is amazing. It is that inner feeling that when something is missing or it is not how you want it, then you can simply create your own. And create Android applications is never been that easy. With this new Android Development series, we are going to teach you what you need to be able to make your dream app on your own. In today’s article, we are going to talk about using AsyncTask to fetch data from an internet connection.

 

AsyncTask allows you to run a task on a background thread while publishing results to the UI thread. We are going to collect some data from a web server through an AsyncTask and then display the results in the main Activity thread.

QUICK CODE

Here you are all the code that you need to develop your first AsvncTask Internet Connection

AsyncTaskInternetConnectionTutorial on Github

FULL TUTORIAL

Let’s create a new project with an empty Activity we can call MainActivity. From here we jump straight to set up our Internet Connection class. We create a new class and call it NetworkUtils. To retrieve data from the internet we need a valid URL to call from which we will get a JSON string with all the information we need in our app. We are going to use the Github API. We set some parameters which we will use to create our URI object.

final static String BASE_URL =
       "https://api.github.com/search/repositories";

final static String PARAM_QUERY = "q";
final static String PARAM_SORT = "sort";
final static String sortBy = "stars";

 

Now we create our URL which we will use to connect to the server. We start from a Uri variable from which we build our URL string. We pass a string variable that is the search term for our query. Then we cast it to an URL object and return it.

public static URL buildUrl(String searchQuery) {
   Uri uri = Uri.parse(BASE_URL).buildUpon()
           .appendQueryParameter(PARAM_QUERY, searchQuery)
           .appendQueryParameter(PARAM_SORT, sortBy)
           .build();

   URL url = null;
   try {
       url = new URL(uri.toString());
   } catch (MalformedURLException e) {
       e.printStackTrace();
   }

   return url;
}

 

 

Then we complete our class by creating a new getResponseFromHttpUrl method which allows us to set up the connection to the server and gets the data from our URL. We use an HttpURLConnection object to open the connection and then we get and InputStream and a Scanner object to collect the data and return it as a JSON string.

public static String getResponseFromHttpUrl(URL url) throws IOException {
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
       InputStream in = urlConnection.getInputStream();

       Scanner scanner = new Scanner(in);
       scanner.useDelimiter("\\A");

       boolean hasInput = scanner.hasNext();
       if (hasInput) {
           return scanner.next();
       } else {
           return null;
       }
   } finally {
       urlConnection.disconnect();
   }
}

 

 

We are done with our NetworkUtils class. The next step is to create our XML layout to display our JSON result. We opted for a ScrollView which contains a FrameLayout. In there we have a TextView to display the end result and a progress bar to show while the AsyncTask thread is processing.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="16dp">

       <TextView
           android:id="@+id/tv_display"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginLeft="8dp"
           android:layout_marginStart="8dp"
           android:layout_marginTop="8dp"
           android:gravity="center"
           android:text="RUN CONNECTION"
           android:textColor="@color/colorPrimaryDark"
           android:textSize="21sp"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toTopOf="parent" />

       <ProgressBar
           android:id="@+id/pb_loading"
           style="?android:attr/progressBarStyle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center" />

   </FrameLayout>

</ScrollView>

Inside the MainActivity class, we create a new class which will extend AsyncTask. The AsyncTask accepts three parameters:

  • Params: Parameter type sent to the task upon execution, in our example a URL type

  • Progress: Type published to update progress during the background computation, in our example an Integer

  • Result: The type of the result of the background computation, in our example our JSON String

public class BackgroundTask extends AsyncTask<URL, Integer, String>

 

Then we override the three main methods which are onPreExecute, doInBackground, onPostExecute.

@Override
protected void onPreExecute()

@Override
protected void onPostExecute(String s)

@Override
protected String doInBackground(URL... urls)

 

doInBackground will handle the connection. We need now to build our URL string. We create a function in the MainActivity class which return our URL built from the NetworkUtils class and which launch the execute method of our AsyncTask.

private void launchBackgroundTask() {
   BackgroundTask task = new BackgroundTask();
   URL sendUrl = NetworkUtils.buildUrl("Taste");
   task.execute(sendUrl);
}

 

The doInBackground now has the URL string and from here we can open our connection by calling getResponseFromHttpUrl.

@Override
protected String doInBackground(URL... urls) {
   String response = null;
   try {
       response = NetworkUtils.getResponseFromHttpUrl(urls[0]);
   } catch (IOException e) {
       e.printStackTrace();
   }

   return response;
}

 

We also make our loading icon visible while the AsyncTask is processing by using the other two methods, onPreExecute, which is called on the main thread once the AsyncTask is executed, and onPostExecute, which is called at the end of the process and gets our string result as a parameter.

@Override
protected void onPreExecute() {
   super.onPreExecute();
   mLoading.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(String s) {
   super.onPostExecute(s);
   mLoading.setVisibility(View.INVISIBLE);
}

 

We have not defined our mLoading variable. We do it now on the onCreate method of the MainActivity. We also create our TextView and launch our launchBackgroundTask function.

private TextView mDisplay;
private ProgressBar mLoading;
private Toast mToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   mDisplay = findViewById(R.id.tv_display);
   mLoading = findViewById(R.id.pb_loading);
   mLoading.setVisibility(View.INVISIBLE);

   launchBackgroundTask();
}

 

We also created a Toast which we will display at the end of the computation. So, we come back to the onPostExecute function and we add the code to display the Toast and the string result.

@Override
protected void onPostExecute(String s) {
   super.onPostExecute(s);
   mLoading.setVisibility(View.INVISIBLE);
   if (mToast != null) mToast.cancel();

   if (s != null && !s.equals("")) {
       mToast = Toast.makeText(MainActivity.this, "Complete", Toast.LENGTH_LONG);
       mDisplay.setText(s);
   } else {
       mToast = Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG);
   }

   mToast.show();
}

 

The last step is to select the data we need from the string. We can use a JSONObject which take our JSON string as a parameter and then allows us to get all the data we need. Therefore, we create a new function getCondition in the MainActivity.

private String getCondition(String JSONString) {
   String result = null;

   try {
       JSONObject jsonObject = new JSONObject(JSONString);
       // JSONObject subObject = jsonObject.getJSONObject("subObj");
       result = jsonObject.getString("total_count");
   } catch (JSONException e) {
       e.printStackTrace();
   }

   return result;
}

 

We can then display the ‘total_count’ in the onPostExecute method to make it stand out.

@Override
protected void onPostExecute(String s) {
   super.onPostExecute(s);
   mLoading.setVisibility(View.INVISIBLE);
   if (mToast != null) mToast.cancel();

   if (s != null && !s.equals("")) {
       mToast = Toast.makeText(MainActivity.this, "Complete", Toast.LENGTH_LONG);
       mDisplay.setGravity(Gravity.LEFT);
       mDisplay.setText(Html.fromHtml("<h1>" + getCondition(s) + "</h1>"));
       mDisplay.append("\n\n" + s);
   } else {
       mToast = Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG);
   }

   mToast.show();
}

Last but not least we need to ask for the INTERNET permission. We just need to add one more line of code in the AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

 

 

Well done. Now if we try to launch our app we will see the loading icon and then once the data has been retrieved we will have our JSON object displayed on the screen.

AsyncTask Internet Connection end Result

You can find the whole project on Github. The code is free and open source.

AsyncTaskInternetConnectionTutorial on Github

 

See you on the next Android Development tutorial.