1

我在我的应用程序中使用 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);
                    }
                }
            }
        });

很明显我必须实现AsyncTasktwitter.getAccountSettings() 但我怎样才能从方法中获得结果AsyncTask?有没有其他方法可以检查身份验证令牌是否过期?或者我怎样才能实现AsyncTask一个方法,它会isAuthenticated()像我使用的那样返回结果。

请指导我。

谢谢你。

4

2 回答 2

1

您可以查看Zwitscher的源代码, 尤其是TwitterLoginActivity

于 2012-05-02T14:57:00.847 回答
0

您很可能在 4.0 设备中遇到此错误,或者如果 http 连接响应时间过长。

如果您不知道 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();

    }

但更好的方法是实现异步任务。

谢谢桑图

于 2012-05-02T15:09:32.827 回答