Hello
I'm loading Tweets from a user account to show in a listview. Now I want to let the users know what's going on while they're waiting. I've implementend Async Task, but for some reason, onPostExcecute is never called. That's why the dialog is never removed.
Can someone give me a hint.. What am I doing wrong?
I can post my TweetAdapterClass if that's needed
This is my AsyncClass
public class ProgressTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
dialog.setTitle("Even geduld...");
dialog.setMessage("De tweets worden ingeladen...");
dialog.show();
}
protected void onPostExecute() {
try {
if (dialog.isShowing()) {
adaptor = new TweetListAdaptor(MainActivity.this, R.layout.tweetitem, loadTweets());
setListAdapter(adaptor);
dialog.dismiss();
}
} catch (Exception e) {
}
}
#Override
protected Void doInBackground(Void... arg0) {
return null;
}
}
LoadTweets looks like this:
private ArrayList<Tweets> loadTweets() {
ArrayList<Tweets> tweets = new ArrayList<Tweets>();
try {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(
"http://search.twitter.com/search.json?q=JobXXL_be&rpp=10");
// HttpGet get = new
// HttpGet("http://search.twitter.com/search.json?q=Stijn_messiaen&rp=10");
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(rp.getEntity());
JSONObject root = new JSONObject(result);
JSONArray sessions = root.getJSONArray("results");
for (int i = 0; i < sessions.length(); i++) {
JSONObject session = sessions.getJSONObject(i);
Tweets tweet = new Tweets();
tweet.setTweet(session.getString("text"));
tweet.setUser(session.getString("from_user"));
// datum vertalen
String date = session.getString("created_at").substring(5,
16);
String[] arrDate = date.split(" ");
int id = this.getResources().getIdentifier(
arrDate[1].toString(), "string",
this.getPackageName());
String maand = getResources().getString(id);
date = arrDate[0].toString() + " " + maand + " "
+ arrDate[2].toString();
tweet.setDate(date);
tweets.add(tweet);
}
}
} catch (Exception e) {
Log.e("TwitterFeedActivity", "Error loading JSON", e);
}
return tweets;
}
EDIT: I added my LoadTweets() in my doInBackgroundMethod and that solved my problems!
Try declaring the ProgressDialog dialog in the main class in which your AsyncTask class exists and only call the dialog.show and dialog.dismiss method in the AsyncTask class.
Use onCreateDialog(int id) in activity to create ProgressDialog. In AsyncTask call:
MainActivity.this.showDialog(PROGRESS_DIALOG_ID);
To dismiss:
MainActivity.this.dismissDialog(PROGRESS_DIALOG_ID);
Dialogs are connected with activity's context. When activity is recreated dialog should be recreated too but then instance of the dialog is not the same.
I had the same problems and yeah this happened to me when onPostExecute didn't match the declaration... try something like protected Void onPostExecute(Void... arg0)
Next thing to do is to put Log.d("Your app", "Location of this code"); and check which part doesn't execute on Log file ...
Hope u will find the solution...
onPostExecute() does not match the declaration Void. I am on the road so off the top of my head consider:
protected void onPostExecute(Void result)
More here.
Hmm, two things come to mind here..
1) Why is your onPostExecute not overridden like the onPreExecute and doInBackground?
2) Why is the doInBackground after the onPost Execute? Generally the order is
onPreExecute
doInBackground
onPostExecute
Related
I am building an application that is pretty dependent on async requests to function.
I have the main Activity called MainActivity. This really doesn't do much apart from contain layouts, however it does have a recycleviewer.
I then have a couple of http requests that are done to populate the recycle viewer.
To do this I have wrote a java class as follows:
public class dataforUi extends AsyncTask<String, String, JsonObject> {
private ArrayList<UiElements> els;
protected void onPreExecute() {
progressDialog.setMessage("Downloading your data...");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
RedditRequests.this.cancel(true);
}
});
}
protected JsonObject doInBackground(String... params) {
Do the http request here, get the result and populate uiElements with it
}
#Override
protected void onPostExecute(JsonObject jsonObject) {
super.onPostExecute(jsonObject);
progressDialog.hide();
}
I have a few more classes like this but hopefully it serves as an example of what I'm trying to do.
Then back in Main Activity, I have this code:
public void getUiElements() {
dataforUi ui = new dataforUi(findViewById(android.R.id.content));
try {
ui.execute("https://t").get();
ArrayList<UiElements> r = ui.getEls();
Log.d("MainFeedScreen", "Size of r is:" + r.size());
UiAdapter = new UiAdapter(r);
mRecyclerView.setAdapter(UiAdapter);
} catch (Exception e) {
}
}
This works fine, but it is very jolty due to the use of .get() on execute to make it blocking. If i remove .get() the progress bar shows up and disappears when the task is done, but my ui thread has progressed past this and ha tried to populate my view with an Empty Array and therefore nothing shows.
I have done a bit of looking into it but cant find a conclusive way of managing the notification of the UI thread that an activity is done.
Would really appericiate any advice on this one.
You need to fix your design.
On post execute, use local broadcast to notify your MainActivity that the AsyncTask is done.
Try using a separate thread to process your data. I use a ListView in stead of a RecyclerView, but the principle is the same.
I have no problems with jolting views.
protected void onPostExecute(String result) {
final String value = result;
// dismiss the dialog after getting all data
progressDialog.dismiss();
if (!value.isEmpty()) {
// updating UI from a new thread
runOnUiThread(new Runnable() {
public void run() {
// ListData is my custom class that holds my data
ArrayList<ListData> arrayDriverListData = new ArrayList<ListData>();
ListDataAdapter adapter = new ListDataAdapter(ListActivity.this, arrayListData);
ListData data;
boolean b = true;
try {
// My data is from a Json source from node 'history'
JSONObject object = new JSONObject(value);
JSONArray array = object.getJSONArray("history");
int len = array.length();
if (len > 0) {
for (int i = 0; i < len; i++) {
final JSONObject o = array.getJSONObject(i);
// Parse my data and add it to my adapter
adapter.add(data);
}
}
} catch (JSONException jex) {
Log.e(TAG, "" + jex.getMessage());
}
// setListAdapter is my call to update my list view
setListAdapter(adapter);
}
});
}
}
Now just update the UI thread
private void setListAdapter(ListDataAdapter adapter){
// my ListView
lvHistory.setAdapter(adapter);
}
I am writing here because this is my last solution of understanding this type of programming.The problem is that I got stuck on what to use to handle the connection to a server and log-in. Should I use async task, handler or thread ? I didn't find a concrete answer stating which one to use, only found that async task is used to download images or other download stuffs.
Until now I have used a thread to connect to the server. The problem I encountered was when I catch the exception ( Putting invalid username/password ) and try to log-in again. ( I needed to "close" the last thread and start one again )
After this I started to use async task but I don't really understand how it should work and I am stuck on a toast of invalid username/password.
private class connectStorage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
api = DefaultClientFactory.create(host, getUser, getPassword);
if (api.getAuthToken().trim().length() > 3) {
//TO DO LAYOUT CHANGE;
}
} catch (StorageApiException e) {
e.printStackTrace();
Log.i("TEST", "" + e.getMessage());
}
return null;
}
Also, I am 100% sure that calling inflate in the doInBackground method won't work too ( there I wanted to change the activity ).
I am starting the async task on a button press.
When you are using asynctask
You have doInBackground and onPostExecute
So basically get a json or string or boolean as a result from doinbackground
and in onpostexecute check if the login in succesful or not if its succesful save the data from server and start an intent to go to another activity or toast the user that that user login details are wrong and try again.
So your asynctask can be an inner class of your activity class which is login and onClickSubmit button call the asynctask class and on post execute parse the json and according to the result decide what to do
Example:
public class SignInAsycTask extends AsyncTask<RequestParams, Void, String> {
#Override
protected String doInBackground(RequestParams... params) {
return new HttpManager().sendUserData(params[0]);
}
#Override
protected void onPostExecute(String result) {
String[] details = parseJsonObject(result);
if (details != null) {
user.setUser_id(Integer.valueOf(details[0]));
user.setName(details[1]);
if (details.length > 2) {
user.setProfilePic(details[2]);
}
setSharedPreferences();
startActivity(new Intent(Signin.this, MainActivity.class));
finish();
} else {
Toast.makeText(Signin.this, "please try again",
Toast.LENGTH_LONG).show();
}
}
}
public String[] parseJsonObject(String result) {
JSONObject obj = null;
try {
obj = new JSONObject(result);
if (obj.has("success")) {
if (obj.getInt("success") == 1) {
if (obj.has("user_pic")) {
return new String[] {
String.valueOf(obj.getInt("user_id")),
obj.getString("user_name"),
obj.getString("user_pic") };
} else {
return new String[] {
String.valueOf(obj.getInt("user_id")),
obj.getString("user_name"), };
}
} else {
return null;
}
} else {
return null;
}
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
here my RequestParams are just a object where I stored all the details like url parameters to send etc and the output of the doinbackground is a String and I am parsing it in my postexecute method
I'm trying to execute AsyncTask but when AsyncTask start and doInBackground finish (value returned), it is skipping the OnPostExecute and running the code requestTask2.execute() below, before i change the value in OnPostExecute, it is trying to run if condition so i'm getting null.
Let me explain with the code :
public void onClick(DialogInterface dialog,int id) {
Intent gt = new Intent(MainActivity.this, favorite.class);
String password = userInput.getText().toString();
String kadi = userInput2.getText().toString();
RequestTask2 requestTask2 = new RequestTask2();
requestTask2.execute("http://www.example.com/androfav/?fav2="+kadi+":"+password).get();
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
// This if condition works before on Post Excecute and it is causing the problem.
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}}
class RequestTask2 extends AsyncTask<String, String, String> {
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Diziler Yükleniyor \n Lütfen Bekleyin...");
dialog.show();
dialog.setCancelable(false);
}
#Override
protected String doInBackground(String... uri2) {
HttpClient httpclient2 = new DefaultHttpClient();
HttpResponse response2;
String responseString2 = null;
try {
response2 = httpclient2.execute(new HttpGet(uri2[0]));
StatusLine statusLine2 = response2.getStatusLine();
if (statusLine2.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response2.getEntity().writeTo(out);
out.close();
responseString2 = out.toString();
} else {
// Closes the connection.
response2.getEntity().getContent().close();
throw new IOException(statusLine2.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString2;
}
#Override
protected void onPostExecute(String result2) {
super.onPostExecute(result2);
try {
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
//............................... Codes
dialog.dismiss();
}
}
How can i wait for OnPostExecute before the if condition works.
Hope i could understand myself.
Thanks in advance.
AsyncTask as the name suggests is Asynchronous. You need to move the if condition to onPostExecute.
Move the below to onPostExecute
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
Edit:
I din't notice you called get(). Calling get() makes Asynctask no more asynchronous. You should never call get() just execute() is enough.
Why do you need to call get() which blocks the ui thread waiting for the task to be finished.
You should always avoid calling get() when using AsyncTask. Instead, do all of your post-processing in onPostExecute
#Override
protected void onPostExecute(String result2) {
super.onPostExecute(result2);
try {
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
}
dialog.dismiss();
}
Below is an async class i created that i am trying to implement a dialog on execute and a Toast on complete.
How ever no toast or dialog are ever showing up.
my asykTask:
public class EmailPictureService extends HTTPRequestAsyncTask {
Context context;
ProgressDialog dialog;
public EmailPictureService(Context context){
this.context = context;
//dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(Object... params) {
Log.v("Start EMAIL SERVICE","START YOPPPPPPPPPPPPPPPPPPPPPP!");
dialog = new ProgressDialog(context);
dialog.setMessage("Sending...");
dialog.setIndeterminate(true);
dialog.show();
HTTPInvoker invoker = new HTTPInvoker();
HttpResponse response = null;
EmailPicture emailPicture = new EmailPicture();
emailPicture.setDeviceType("TABLET");
emailPicture.setStoreId((String)params[1]);
emailPicture.setNotificationType("E");
emailPicture.setRecipientName((String)params[2]);
emailPicture.setRecipientEmail((String)params[3]);
String jsonString = JSONConverter.toJson(emailPicture);
response = invoker.invokePOSTFileService((String)params[0], jsonString, (File[])params[4]);
return parseHttpResponse(response);
}
#Override
protected void onPostExecute(String result) {
String msg = "";
if (dialog.isShowing()) {
dialog.dismiss();
}
if (result != null) {
JSONObject jsonObject = null;
long errorCode = 0;
try {
jsonObject = new JSONObject((String) result);
errorCode = jsonObject.getLong("errorCode");
if(errorCode<1){
msg ="Success, your picture has been sent";
}else{
msg = "Sorry, there was an error sending your picture. Please try again later.";
}
Log.i(Constants.TAG, "Error Code...." + errorCode);
Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
toast.show();
} catch (JSONException e1) {
Log.i(Constants.TAG, "Exception...." + e1);
Toast toast = Toast.makeText(context, "Failure: "+e1, Toast.LENGTH_SHORT);
toast.show();
e1.printStackTrace();
}
}
}
}
how i call it from my activity:
new EmailPictureService(this).execute(url,storeID,cusName, cusEmail, new File[]{file});
my log
You should not attempt to access the UI from doInBackground(). The purpose of AsyncTasks and doInBackground() is avoid bogging down the UI thread... Instead you should preform the UI work in the appropriate methods: onPreExecute(), onProgressUpdate(), onPostExecute(), etc
I suspect the toast isn't showing because your result is always null. Your log shows an error on the post
As others have said, start your progress dialog from onPreExecute()
I note that you instantiate your progressDialog in doInBackground(). Move it to onPreExecute() instead. doInBackground() are only supposed to do non-UI work. =)
This should "solve" your problem.
I have an AsyncTask which loads Tweets from Twitter.
I also have a PullToRefresh ListView... Whenever i pull to refresh it, the listview immediately clears and as soon as the data has been loaded, it's getting filled into the listview.
I have other ListViews in my App all with the same stuff (PullToRefresh and Async data loading...). On the other ListViews this does not happen. Only on the Twitter ListView. What am I doing wrong?
Here is my Code:
public class TwitterDownloader extends AsyncTask<Void, Void, String> {
final Handler mHandler = new Handler();
public TwitterDownloader() {
}
#Override
public void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params) {
twitter4j.Twitter twitter = new TwitterFactory().getInstance();
listTweets.clear();
List<twitter4j.Status> statuses = null;
try {
statuses = twitter.getUserTimeline(
MainActivity.TWITTER_USERNAME, new Paging(1, 50));
} catch (TwitterException e) {
e.printStackTrace();
Log.e(MainActivity.LOG_TAG, "TwitterException");
}
try {
for (twitter4j.Status status : statuses) {
listTweets.add(status.getText());
}
} catch (NullPointerException npe) {
}
return null;
}
#Override
public void onPostExecute(String unused) {
MyCustomAdapter myAdapter = new MyCustomAdapter(myContext,
R.layout.row_twitter, listTweets);
setListAdapter(myAdapter);
getListView().setTextFilterEnabled(true);
String lastUpdate = (new SimpleDateFormat(
"HH:mm")).format(new Date());
pullToRefreshView.onRefreshComplete();
pullToRefreshView.setLastUpdatedLabel(getString(R.string.last_updated) + ": "
+ lastUpdate);
}
I am not sure about this but in doInBackground method of AsyncTask, you are doing listTweets.clear();. After getting result, you are adding data to it. May be this is causing problems.
I finally fixed it by adding all my clear() statements right before I fill up my list again (which is inside a try catch).
So the new code inside my doInBackground method is:
try {
listTweets.clear();
listUsernames.clear();
listDates.clear();
listImageURLs.clear();
listURLsOfTweets.clear();
for (twitter4j.Status status : statuses) {
listTweets.add(status.getText());
listUsernames.add(status.getUser().getName());
listDates.add(android.text.format.DateFormat
.getDateFormat(getApplicationContext()).format(
status.getCreatedAt())
+ " "
+ android.text.format.DateFormat.getTimeFormat(
getApplicationContext()).format(
status.getCreatedAt()));
listImageURLs.add(status.getUser().getProfileImageURL()
.toString());
StringBuffer address = new StringBuffer();
address.append("http://twitter.com/#!/");
address.append(status.getUser().getScreenName());
address.append("/status/");
address.append(status.getId());
listURLsOfTweets.add(address.toString());
}