微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 方法retrieveRequestToken引发“与服务提供商通信失败:null”

我使用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();
    }
}

谢谢.

解决方法

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

ICS不允许在UI线程中发生Http请求,所以当你这样做时,你会得到这个错误.

解决这个问题,请做provider.retrieveRequestToken(consumer,CALLBACK_URL);在后台线程以及provider.retrieveAccesstoken(consumer,verifier);.

资料来源:http://code.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

原文地址:https://www.jb51.cc/android/311041.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐