Reputation: 11
i am working on an AsyncTask but after the doInBackground is completed onPostExecute doesnt gets called.
Here's my AsyncTask class
public class GetServerData extends AsyncTask<Object, String, String>{
Context con;
ArrayList<Items> data;
ListViewAdapter adapter;
String IMAGE_PREFIX, cat_id;
public GetServerData(Context con, ArrayList<Items> data, ListViewAdapter adapter, String cat_id){
this.con = con;
this.data = data;
this.adapter = adapter;
IMAGE_PREFIX = con.getResources().getString(R.string.web_image_prefix);
this.cat_id = cat_id;
}
@Override
protected String doInBackground(Object... arg0) {
// TODO Auto-generated method stub
String url = (String) arg0[0];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = null;
if (!(((Activity) con) instanceof MainActivity)){
nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("limit", (String) arg0[1]));
nameValuePairs.add(new BasicNameValuePair("cat_id", cat_id));
}else if (((Activity) con) instanceof MainActivity){
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("limit", (String) arg0[1]));
}
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}catch (Exception e){;
}
String result = null;
HttpEntity entity = response.getEntity();
try {
result = EntityUtils.toString(entity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(con, "Success", Toast.LENGTH_SHORT).show();
}
}
and further more this is how i am executing the task
new GetServerData(this, data, adapter, CPATH).execute(URL, ""+current_limit);
the doInBackground method runs perfectly and does gets data as it should. But i need to post process the data in onPostExecute. But it doesnt gets called. What am i doing wrong here ?
Upvotes: 0
Views: 169
Reputation: 11
The problem solved in a very unexpected way. Restarting eclipse fixed the problem as i looked into in the Eclipse wasn't building the project properly.
Upvotes: 0
Reputation: 5151
}catch (Exception e){
Toast.makeText(con, "Unable to fetch data", Toast.LENGTH_SHORT).show();
}
The Toast.makeText in the last catch block is throwing the exception. I think this makes it pretty clear that you should not be attempting to display toasts from a background thread.
You should be able to see the stack trace for this in logcat.
Upvotes: 1
Reputation: 45490
You are displaying toast messages in doInbackground, thats a violation of async task all ui related stuff should go in postExecute.
Please remove these lines.
Upvotes: 1