Home » Android

Android Application Connectivity with the Server

In this article, we will learn how to connect an Android application with the server of any kind?
Submitted by Manu Jemini, on January 13, 2018

App connectivity with the SERVER in Android

Image source: https://www.safaribooksonline.com/library/view/hacking-android/9781785883149/graphics/B05065_06_01.jpg

Your android need to connect with a server to access data-base files such as images, records etc. If your app is not Stand-alone then you got to connect your app with a server.

In this Example we will look at how to make HTTP request from the mobile device to the remote server. HTTP Requests are of several different types but the important and must know types are GET and POST.

To make a HTTP Request you will need to have either a library or you can also do it with a JAVA file.

First Step → Copy the code below and paste it in a separate file with a name CallHttpRequest

package com.example.sandeepsappal.friendcircle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class CallHttpRequest extends AsyncTask<String,Void,String> {
    Context context;
    String process;
    File fileupload;
    Bitmap bitmap;
     public  CallHttpRequest(Context context,String process)

    protected String doInBackground(String... url) {
          String output= FetchImage(url[0]);
        	return output;
        return  null;

    protected void onPostExecute(String s) {
    String callRequest(String Url) {
        try {
            URL url = new URL(Url);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            DataInputStream in=new DataInputStream(con.getInputStream());

            StringBuffer output = new StringBuffer();
            String str;
            while ((str = in.readLine()) != null) {
            return (output.toString());

        } catch (Exception e) {
          //  Toast.makeText(ctx, "Error:" + e, Toast.LENGTH_SHORT).show();
        return (null);

This file consists of a constructor, doInBackground function, callRequest function and onPostExecute function.


This part is used to store data for the operation like URL, a string or an Object model.

public  CallHttpRequest(Context context,String process){

As you can see we are taking two parameters and storing them in the object. This is very beneficial when it comes to the long run. As the object will consist of process with which we can decide what to do for a particular process.


This function is Asynchronous in nature, which gives it name as do-in-background. In this function we decide which function to call. Right now assume that you want to fetch something from the server so you want to use another function rather than uploading a file.

protected String doInBackground(String... url) {
		String output= FetchImage(url[0]);
		return output;
	}else if(process.equals("something")){
		// do something else
	return  null;


This function will call after the doInBackground function. Here you will have the result and you can do anything with it like refreshing the list.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.