GenericAsyncTaskLib

In most of the android applications it is essential that app may need to connect to internet and make some HTTP requests. In this article i’ll be demonstrating about making simple HTTP Requests in android.

GenericAsyncTaskLib (also known as Generic AsyncTask Llibrary) is a Java library to make asynchronous HTTP call throughtout application .It can also be used to convert a JSON string to an equivalent Java object.It can perform variation operation as GET,POST,PUT,DELETE,File Download & File Upload to server.


Creating HTTP Client and HTTP Post :

MethodType : GET

String urlGET = “http://jsonplaceholder.typicode.com/comments”

CustomAsyncTask mAsyncTask = new CustomAsyncTask(MainActivity.this,urlGET, “GET”);

mAsyncTask.execute();

 MethodType : POST

Login mLogin = new Login();
mLogin.setUserName(“abc”);
mLogin.setPassWord(“abc123”);

/* Create Object & pass to create JSONObject */
String jsonObject = ServiceCall.createRequest(mLogin);
String urlPOST = “http://192.168.10.100:8080/api/login”;
CustomAsyncTask mAsyncTask = new CustomAsyncTask(MainActivity.this,urlPOST, “POST” ,jsonObject);
mAsyncTask.execute();

But if Request is NameValuePair :

// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("password", "encrypted_password"));

File Download :

String urlDOWNLOAD = “http://api.androidhive.info/progressdialog/hive.jpg&#8221;;

String pathToSaveFile = “/sdcard/test.jpg”;

CustomAsyncTask mAsyncTask = new CustomAsyncTask(MainActivity.this,urlDOWNLOAD,pathToSaveFile,”DOWNLOAD”);

mAsyncTask.execute();

File Upload:

String urlUPLOAD = “http://192.168.56.1:8080/api/uploadImager&#8221;;

String uploadedFilePath = “Uploded File path”;

CustomAsyncTask mAsyncTask = new CustomAsyncTask(MainActivity.this, urlUPLOAD,uploadedFilePath,”UPLOAD”);

mAsyncTask.execute();

Progress Dialog :

Progress Dialog can be shown using ProgressDialog class. It is a subclass of normal AlertDialog class. So add an alert method in your main activity class.

private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
progressDialog(progress_bar_type);

private void progressDialog(int progress_bar_type){

switch (progress_bar_type) {
case 0:
pDialog = new ProgressDialog(this);
pDialog.setMessage(“Please wait…”);
pDialog.setCancelable(false);
pDialog.show();
break;
case 1:
pDialog = new ProgressDialog(this);
pDialog.setMessage(“Downloading file. Please wait…”);
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
break;
case 2:
pDialog = new ProgressDialog(this);
pDialog.setMessage(“Uploading file. Please wait…”);
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
break;

default:
break;
}
}

to stop progress dialog :

@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
pDialog.dismiss();
}

To show Progress on Percentage :

@Override
public void onProgressUpdate(Integer progress) {
pDialog.setProgress(progress);
}

Response after execution :

“implements AsyncTaskCompleteListener” .to get response.

Ex.

@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
pDialog.dismiss();
}

@Override
public void onProgressUpdate(Integer progress) {
pDialog.setProgress(progress);
}

Finally Convert result String to java Object.

Ex.It can be list or object.

List<Response> list = ServiceCall.getResponse(result, Login.class);

Response  response = ServiceCall.getResponse(result, Login.class);

Open your AndroidManifest.xml file and add internet connect permission and writing to sdcard permission.

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.RECORD_AUDIO” />

DownLoad Link : https://github.com/Sabinash/GenericAsyncLib