Android Networking Tutorial

1- Introduction

This document is based on:
  • Android Studio 1.5

2- Android Networking Overview

In Android, Network programming includes a request to the server and retrieve the data returned. Basically you have two APIs for working with the network:

Apache HttpClient:

  • This is an open source library provided by the Apache.

HttpURLConnection:

  • This is an official  API of Android, it began to be included in the version of Android 2.3, in the previous, Android using Apache HttpClient to work with the network.
You need to grant some permissions to the application if you want to work with the network
 
<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
android.permission.INTERNET:
  • Add this permission, allowing your application will have the ability to connect to the network.

android.permission.ACCESS_NETWORK_STATE:
  • Allows the app to check the status of your network connection.

The following code checks the network connection status:
 private boolean checkInternetConnection() {
  
      ConnectivityManager connManager =
              (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    
      NetworkInfo networkInfo = connManager.getActiveNetworkInfo();

      if (networkInfo == null) {
          Toast.makeText(this, "No default network is currently active", Toast.LENGTH_LONG).show();
          return false;
      }

      if (!networkInfo.isConnected()) {
          Toast.makeText(this, "Network is not connected", Toast.LENGTH_LONG).show();
          return false;
      }

      if (!networkInfo.isAvailable()) {
          Toast.makeText(this, "Network not available", Toast.LENGTH_LONG).show();
          return false;
      }
      Toast.makeText(this, "Network OK", Toast.LENGTH_LONG).show();
      return true;
  }

3- NetworkOnMainThreadException

By default, when you work with a network in Android, you should create a new thread to send and receive data returned. If you work on the main thread you will get android.os.NetworkOnMainThreadException, this is the default policy of android. However you can override this policy of Android to be able to work with the Network on the main thread.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);
It is recommended that you create a class extending from AsyncTask <Params, Progress, Result>, this class is not a thread, it extends from Object, but when to do their task (Call   AsyncTask.execute(params) method), it creates a new thread to execute the  doInBackground (params) method. After thread is completed, onPostExecute (result) method will be called.

You can see the illustration below:

4- HttpURLConnection Example

In this example, you download the Image and json from URL and displays it on the ImageView and TextView.
Create project AndroidNetworkingDemo:
Project is created.
You must grant permissions to the app to access the Internet and the permission to check the status of the network.
<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Add to AndroidManifest.xml:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.o7planning.androidnetworkingdemo">

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Apps interface:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="org.o7planning.androidnetworkingdemo.MainActivity">

   <ImageView
       android:layout_width="fill_parent"
       android:layout_height="120dp"
       android:id="@+id/imageView"
       android:layout_marginTop="10dp" />

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="220dp"
       android:textAppearance="?android:attr/textAppearanceSmall"
       android:text="Small Text"
       android:id="@+id/textView"
       android:layout_below="@+id/imageView"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="10dp" />

   <Button
       style="?android:attr/buttonStyleSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Download Image"
       android:id="@+id/button"
       android:layout_below="@+id/textView"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:layout_marginTop="37dp"
       android:onClick="downloadAndShowImage" />

   <Button
       style="?android:attr/buttonStyleSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Download Json"
       android:id="@+id/button2"
       android:layout_alignTop="@+id/button"
       android:layout_toRightOf="@+id/button"
       android:layout_toEndOf="@+id/button"
       android:onClick="downloadAndShowJson" />

</RelativeLayout>

Code:

IOUtils.java
package org.o7planning.androidnetworkingdemo;


import java.io.InputStream;
import java.io.Reader;

public class IOUtils {

   public static void closeQuietly(InputStream in)  {
       try {
           in.close();
       }catch (Exception e) {

       }
   }

   public static void closeQuietly(Reader reader)  {
       try {
           reader.close();
       }catch (Exception e) {

       }
   }

}
DownloadJsonTask.java
package org.o7planning.androidnetworkingdemo;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

// A task with String input parameter, and returns the result as String.
public class DownloadJsonTask
       // AsyncTask<Params, Progress, Result>
        extends AsyncTask<String, Void, String> {

    private TextView textView;

    public DownloadJsonTask(TextView textView)  {
        this.textView= textView;
    }

    @Override
    protected String doInBackground(String... params) {
        String textUrl = params[0];

        InputStream in = null;
        BufferedReader br= null;
        try {
            URL url = new URL(textUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            int resCode = httpConn.getResponseCode();

            if (resCode == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
                br= new BufferedReader(new InputStreamReader(in));

                StringBuilder sb= new StringBuilder();
                String s= null;
                while((s= br.readLine())!= null) {
                    sb.append(s);
                    sb.append("\n");
                }
                return sb.toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(br);
        }
        return null;
    }

    // When the task is completed, this method will be called
    // Download complete. Lets update UI
    @Override
    protected void onPostExecute(String result) {
        if(result  != null){
            this.textView.setText(result);
        } else{
            Log.e("MyMessage", "Failed to fetch data!");
        }
    }
}
DownloadImageTask.java
package org.o7planning.androidnetworkingdemo;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

// A task with String input parameter, and returns the result as bitmap.
public class DownloadImageTask
        // AsyncTask<Params, Progress, Result>
        extends AsyncTask<String, Void, Bitmap> {

    private ImageView imageView;

    public DownloadImageTask(ImageView imageView)  {
        this.imageView= imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        String imageUrl = params[0];

        InputStream in = null;
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            int resCode = httpConn.getResponseCode();

            if (resCode == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            } else {
                return null;
            }

            Bitmap bitmap = BitmapFactory.decodeStream(in);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
        return null;
    }

    // When the task is completed, this method will be called
    // Download complete. Lets update UI
    @Override
    protected void onPostExecute(Bitmap result) {
        if(result  != null){
            this.imageView.setImageBitmap(result);
        } else{
            Log.e("MyMessage", "Failed to fetch data!");
        }
    }
}
MainActivity.java
package org.o7planning.androidnetworkingdemo;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   private ImageView imageView;
   private TextView textView;

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

       this.imageView = (ImageView) this.findViewById(R.id.imageView);
       this.textView = (TextView) this.findViewById(R.id.textView);
   }


   private boolean checkInternetConnection() {
       // Get Connectivity Manager
       ConnectivityManager connManager =
               (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

       // Details about the currently active default data network
       NetworkInfo networkInfo = connManager.getActiveNetworkInfo();

       if (networkInfo == null) {
           Toast.makeText(this, "No default network is currently active", Toast.LENGTH_LONG).show();
           return false;
       }

       if (!networkInfo.isConnected()) {
           Toast.makeText(this, "Network is not connected", Toast.LENGTH_LONG).show();
           return false;
       }

       if (!networkInfo.isAvailable()) {
           Toast.makeText(this, "Network not available", Toast.LENGTH_LONG).show();
           return false;
       }
       Toast.makeText(this, "Network OK", Toast.LENGTH_LONG).show();
       return true;
   }

   // When user click on the "Download Image".
   public void downloadAndShowImage(View view) {
       boolean networkOK = this.checkInternetConnection();
       if (!networkOK) {
           return;
       }
       String imageUrl = "http://o7planning.org/download/static/default/demo-data/logo.png";

       // Create a task to download and display image.
       DownloadImageTask task = new DownloadImageTask(this.imageView);

       // Execute task (Pass imageUrl).
       task.execute(imageUrl);
   }

   // When user click on the "Download Json".
   public void downloadAndShowJson(View view) {
       boolean networkOK = this.checkInternetConnection();
       if (!networkOK) {
           return;
       }
       String jsonUrl = "http://o7planning.org/download/static/default/demo-data/company.json";

       // Create a task to download and display json content.
       DownloadJsonTask task = new DownloadJsonTask(this.textView);

       // Execute task (Pass jsonUrl).
       task.execute(jsonUrl);
   }


}
Running apps: