I have found the way to perform background task in non UI thread. for that you have to extends AsyncTask and have to implement doInBackground method. to know more on AsyncTask you can refer android developer website.
here is my code I have done with.
public class BackgroundTask extends AsyncTask<Object, Integer, String> {
private final static String SERVICE_URI = "http://192.168.0.105:80/Service1.svc/";
//runs after doInBackground completed
protected void onPostExecute(String result) {
Register_activity.emailV.setText(result);
}
@Override
protected String doInBackground(Object... params) {
Bitmap image = (Bitmap)params[1];
String msg = new String();
String methodname = params[0].toString();
try {
byte[] data = SaveImage.toByteArray(image);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(SERVICE_URI+methodname);
postRequest.setHeader("Content-type","application/octet-stream");
ByteArrayBody bab = new ByteArrayBody(data, Register_activity.mobileV.getText().toString()+".jpg");
// File file= new File("/mnt/sdcard/forest.png");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
msg = s.toString();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
return msg;
}
}
here AsyncTask have 3 datatype :
Object represents the type of array data which will be passed to doInBackground method.
Integer represents the type of data which will be used to pass into onProgressUpdate Method.
String represents the type of data which will be used to pass into onPostExecute Method.
there are more options to perform tasks on NonUI thread like java thred. Options are yours.
Happy Coding...
Regards,
Sourabh