我在我的应用程序中使用twitter4j。我使用下面的代码来检查身份验证令牌是否已过期或用户尚未登录
public static boolean isAuthenticated(Context context) {
TwitterPrefrences twitterPref = new TwitterPrefrences(context);
String token = twitterPref.getTwitterAuthToken();
String secret = twitterPref.getTwitterAuthTokenSecrate();
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.TWITT_CONSUMER_KEY, Constants.TWITT_CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
twitter.getAccountSettings();
return true;
} catch (TwitterException e) {
return false;
}
}
但我收到了以下错误
05-02 19:52:23.531: E/AndroidRuntime(1441): FATAL EXCEPTION: main
05-02 19:52:23.531: E/AndroidRuntime(1441): android.os.NetworkOnMainThreadException
05-02 19:52:23.531: E/AndroidRuntime(1441): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-02 19:52:23.531: E/AndroidRuntime(1441): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-02 19:52:23.531: E/AndroidRuntime(1441): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-02 19:52:23.531: E/AndroidRuntime(1441): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-02 19:52:23.531: E/AndroidRuntime(1441): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
05-02 19:52:23.531: E/AndroidRuntime(1441): at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:45)
05-02 19:52:23.531: E/AndroidRuntime(1441): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:178)
05-02 19:52:23.531: E/AndroidRuntime(1441): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:75)
05-02 19:52:23.531: E/AndroidRuntime(1441): at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:103)
05-02 19:52:23.531: E/AndroidRuntime(1441): at
twitter4j.Twitter.getAccountSettings(Twitter.java:1440)
来电者密码为
chbTwOption.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
boolean showTwlogin = false;
if ("No".equalsIgnoreCase(twitPref.getTwitterAuthToken())
|| "No".equalsIgnoreCase(twitPref.getTwitterAuthTokenSecrate())) {
showTwlogin = true;
} else {
showTwlogin = TwitterUtils.isAuthenticated(AddSchedule.this);
}
if(showTwlogin) {
showSocialMediaLoginMessage(Constants.TW_LOGIN_MESSAGE);
}
}
}
});
很明显,我必须为AsyncTask
实施twitter.getAccountSettings()
,但如何从AsyncTask
获取结果作为方法?有没有其他方法来检查身份验证令牌到期?或者我如何实现AsyncTask
作为一种方法,将结果返回为isAuthenticated()
。
请指导我。
谢谢。
答案 0 :(得分:1)
您可以查看Zwitscher的来源 特别是在TwitterLoginActivity
答案 1 :(得分:0)
您可能最有可能在4.0设备中出现此错误,或者如果http conncetion需要太长时间才能响应。
如果你不知道asynctask,那么简单的解决方案就是
public static boolean isAuthenticated(Context context) {
new Thread() {
@Override
public void run() {
TwitterPrefrences twitterPref = new TwitterPrefrences(context);
String token = twitterPref.getTwitterAuthToken();
String secret = twitterPref.getTwitterAuthTokenSecrate();
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.TWITT_CONSUMER_KEY,Constants.TWITT_CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
try {
twitter.getAccountSettings();
return true;
} catch (TwitterException e) {
return false;
}
}
}.start();
}
但更好的方法是实现asynctask。
感谢 santhu