我在我的应用程序中使用了asyncTask,但它没有调用onPostExecute方法。我读了十几遍,找不到错误。
它像这样流动:
这是完整的类和AsyncTask:
package com.vdlow.socialnotification.twitter;
import java.util.ArrayList;
import com.vdlow.socialnotification.R;
import oauth.signpost.OAuth;
import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class TwitterUtils {
static long userID;
static Context c;
static Boolean retornoSend;
public static boolean isAuthenticated(SharedPreferences prefs) {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
twitter.getAccountSettings();
return true;
} catch (TwitterException e) {
return false;
}
}
public static void sendTweet(SharedPreferences prefs,String msg, Context cs) throws Exception {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
String values[] = {token, secret, msg};
TwitterUtils tu = new TwitterUtils();
TweetSender ts = tu.new TweetSender();
ts.execute(values);
Log.i("retornoSend", retornoSend.toString());
if(retornoSend){
Toast.makeText(cs, cs.getString(R.string.tweet_sent), Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(cs, cs.getString(R.string.fail_sending), Toast.LENGTH_LONG).show();
}
}
public static ArrayList<String> getUserId(final SharedPreferences prefs) throws Exception{
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
userID = twitter.getId();
} catch (IllegalStateException e) {
Log.e("Error! Method: getUserId() Class: TwitterUtils", e.toString());
}
Log.i("USERID", String.valueOf(userID));
ArrayList<String> following = new ArrayList<String>();
long lCursor = -1;
try{
userID = twitter.getId();
IDs friendsIDs = twitter.getFriendsIDs(userID, lCursor);
do
{
for (long i : friendsIDs.getIDs())
{
following.add("@"+twitter.showUser(i).getScreenName());
}
}while(friendsIDs.hasNext());
}
catch(Exception e){
Log.e("Exception! Class TwitterUtils Method: getFriendsName()", e.toString()+"\n User ID "+String.valueOf(userID));
}
return following;
}
private class TweetSender extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... msg) {
Looper.prepare();
String token = msg[0];
String secret = msg[1];
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
twitter.updateStatus(msg[2]);
} catch (TwitterException e) {
Log.e("Error sending tweet Class TwitterUtils Method sendTweet() AsyncTask TweetSender", e.toString());
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result){
Log.i("Post execute", "Runned");
retornoSend = result;
}
}
为什么它不起作用的任何想法?
答案 0 :(得分:0)
您的doInBackground
要么永远不会完成,要么在某个时候取消请求:
来自开发者文档(请参阅下面的粗体文字):http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
尝试取消执行此任务。如果这种尝试失败了 任务已经完成,已经取消,或者不能 由于其他原因被取消。如果成功,这个任务就有了 调用cancel时未启动,此任务永远不会运行。如果 任务已经启动,然后是mayInterruptIfRunning参数 确定执行此任务的线程是否应该是 因试图停止任务而中断。
调用此方法将导致调用onCancelled(Object) 在doInBackground(Object [])返回后的UI线程上。的调用 此方法保证永远不会调用onPostExecute(Object)。 调用此方法后,应检查返回的值 isCancelled()定期从doInBackground(Object [])来完成 尽早完成任务。