本文介绍了方法retrieveRequestToken引发"通信与服务提供者失败:空"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用twitter4j从我的应用程序发送的tweet。当我调用该方法retrieveRequestToken,我得到错误的服务提供者的通信失败:空

I am using twitter4j to send tweet from my application. When I invoke the method retrieveRequestToken, I get the error "Communication with the service provider failed: 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();
    }
}

感谢你。

推荐答案

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

If this is in ICS, I highly recommend against using StrictMode.enableDefaults;.

ICS不允许HTTP请求发生在UI线程,所以当你如上这样做,你得到这个错误。

ICS does not allow Http requests to occur in the UI thread, so when you do as above, you get that error.

要解决这个问题,做 provider.retrieveRequestToken(消费者,CALLBACK_URL); 在后台线程以及 provider.retrieveAccessToken(消费者,验证);

To fix this, do provider.retrieveRequestToken(consumer, CALLBACK_URL); in a background thread as well as provider.retrieveAccessToken(consumer, verifier);.

来源:<一href="http://$c$c.google.com/p/oauth-signpost/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=71">http://$c$c.google.com/p/oauth-signpost/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=71

这篇关于方法retrieveRequestToken引发&QUOT;通信与服务提供者失败:空&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 02:46