方法retrieveRequestToken引发"与服务提供者的通信失败:null"

Gui*_*bar 9 android twitter4j

我正在使用twitter4j从我的应用程序发送推文.当我调用方法retrieveRequestToken时,我收到错误"与服务提供者的通信失败:null".

public static void askOAuth(Context context) {
    try {
        // Use Apache HttpClient for HTTP messaging
        consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        provider = new CommonsHttpOAuthProvider(
                "https://api.twitter.com/oauth/request_token",
                "https://api.twitter.com/oauth/access_token",
                "https://api.twitter.com/oauth/authorize");
        String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
        Toast.makeText(context, "Authorize this app!", Toast.LENGTH_LONG).show();
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    } catch (Exception e) {
        Log.e(APP, e.getMessage());
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Gra*_*hew 12

如果这是在ICS,我强烈建议不要使用StrictMode.enableDefaults;.

ICS不允许在UI线程中发生Http请求,因此当您执行上述操作时,您会收到该错误.

要解决这个问题,请provider.retrieveRequestToken(consumer, CALLBACK_URL);在后台线程中执行此操作provider.retrieveAccessToken(consumer, verifier);.

资料来源:http://code.google.com/p/oauth-signpost/issues/detail?scan = 2&start = 0&num = 100&q =&cfpec = ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id= 71