How to Connect Android App to Web Server?

Android | Connect to the server: In this tutorial, we will learn how to connect an Android application with a server of any kind. By Manu Jemini Last updated : June 08, 2023

App connectivity with the SERVER in Android

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.

Connect an Android App to Web 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) {
    this.context = context;
    this.process = process;
  }

  @Override
  protected String doInBackground(String...url) {
    if (process.equals("FetchData")) {
      String output = FetchImage(url[0]);
      return output;
    }
    return null;
  }

  @Override
  protected void onPostExecute(String s) {
    super.onPostExecute(s);
  }
  String callRequest(String Url) {
    try {
      //Toast.makeText(ctx,Url,Toast.LENGTH_LONG).show();
      URL url = new URL(Url);
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setDoInput(true);
      DataInputStream in = new DataInputStream(con.getInputStream());

      StringBuffer output = new StringBuffer();
      String str;
      while ((str = in.readLine()) != null) {
        output.append(str);
      }
      in.close();
      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.

Constructor

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

public CallHttpRequest(Context context, String process) {
  this.context = context;
  this.process = 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.

doInBackground

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.

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

onPostExecute

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.