Friday 10 April 2015

Android AsyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask's generic types


The three types used by an asynchronous task are the following:
  1. Params, the type of the parameters sent to the task upon execution.(Which we passing into  doInBackground(Object[])
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation.


package com.example.mysmpleappforasync;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;

public class MyAsync extends AsyncTask<String, Void, Object>{

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("onCancelled obj", "onCancelled"+result);

}

@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}

@SuppressLint("NewApi") @Override
protected void onCancelled(Object result) {
// TODO Auto-generated method stub
super.onCancelled(result);
Log.e("onCancelled obj", "onCancelled"+result);
}

@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
Log.e("onCancelled", "onCancelled");

}

@Override
protected Object doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient hc=new DefaultHttpClient();
HttpPost request=new HttpPost("http://www.mena-properties.com/mena_app/mobilemanager/getRows");
String json="";
JSONObject jobj=new JSONObject();
try {
jobj.accumulate("param","photos");
jobj.accumulate("start", 0);
jobj.accumulate("noofrows", 10);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json=jobj.toString();
try {
StringEntity se=new StringEntity(json);
request.setEntity(se);
request.setHeader("Accept", "json/application");
request.setHeader("Content-type", "json/application");
try {
HttpResponse res=hc.execute(request);
String jsonResponse = EntityUtils.toString(res.getEntity());
           JSONObject jsobj=new JSONObject(jsonResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}