Issue
I have my separate web service class in which i just pass response method, url and array list of data which is required for making the request and getting the response. I call this web service in my login activity like this
JifWebService webServices = new JifWebService();
webServices.Execute(RequestMethod.POST,
Jifconstant.LOGIN_URL, null, logindata);
loginResponse = webServices.getResponse();
loginResponseCode = webServices.getResponseCode();
In this login data is a array list which contains some data. Now i want to call this web service in background using async task. But i am just not getting it correctly. My web service logic is written in totally different java file and its working fine but i want to call my web service methods inside async task.enter code here
Solution
You can try below code for Async Task and also call web service in doInBackground:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
public class AsyncExample extends Activity{
private String url="http://www.google.co.in";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AsyncCaller().execute();
}
private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("Loading...");
pdLoading.show();
}
@Override
protected Void doInBackground(Void... params) {
//this method will be running on a background thread so don't update UI from here
//do your long-running http tasks here, you don't want to pass argument and u can access the parent class' variable url over here
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//this method will be running on UI thread
pdLoading.dismiss();
}
}
}
Done
Answered By - Hiren Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.