Android 一切似乎都正常,但函数retrieveRequestToken()抛出';OAuthCommunicationException';例外
我正在尝试构建一个android应用程序,以便它在twitter上发布推文。Android 一切似乎都正常,但函数retrieveRequestToken()抛出';OAuthCommunicationException';例外,android,oauth,twitter,twitter-oauth,Android,Oauth,Twitter,Twitter Oauth,我正在尝试构建一个android应用程序,以便它在twitter上发布推文。 我试图使用函数retrieveRequestToken(consumer,CALLBACK_uri)获取uri,但它抛出“OAuthCommunicationException”异常。 我为这个应用程序授予了互联网使用权限(我已经检查过了)。我已经在twitter上注册了这个应用程序,提供读、写和直接消息 Logcat 01-07 192624.589 Din(581) check1 01-07 192624.691
我试图使用函数retrieveRequestToken(consumer,CALLBACK_uri)获取uri,但它抛出“OAuthCommunicationException”异常。 我为这个应用程序授予了互联网使用权限(我已经检查过了)。我已经在twitter上注册了这个应用程序,提供读、写和直接消息
Logcat
01-07 192624.589 Din(581) check1
01-07 192624.691 Dcommunication(581) 4
01-07 192624.691 WSystem.err(581) oauth.signpost.exception.OAuthCommunicationException Communication with the service provider failed null
01-07 192624.691 WSystem.err(581) at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java214)
01-07 192624.702 WSystem.err(581) at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java69)
下面是代码片段
update = (Button) findViewById(R.id.update);
status = (TextView) findViewById(R.id.status);
private static final String CALLBACK_URI = OauthTwittertwitt;
private static final String REQUEST_TOKEN_URL = httpsapi.twitter.comoauthrequest_token;
private static final String ACCESS_TOKEN_URL = httpsapi.twitter.comoauthaccess_token;
private static final String AUTHORIZE_URL = httpsapi.twitter.comoauthauthorize;
private static CommonsHttpOAuthConsumer consumer;
private static DefaultOAuthProvider provider;
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTHORIZE_URL);
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showMyDialog();
try {
Log.d(in,check1);
String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
Log.d(uri, authUrl);
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(authUrl)));
} catch (OAuthMessageSignerException e) {
TODO Auto-generated catch block
Log.d(signerg,1);
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
TODO Auto-generated catch block
Log.d(Notauths,2);
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
TODO Auto-generated catch block
Log.d(authExceg,3);
e.printStackTrace();
} catch (OAuthCommunicationException e) {
TODO Auto-generated catch block
Log.d(communication,4);
e.printStackTrace();
}
}
这是由主线程上的网络异常引起的。尝试在线程上运行oauth代码:
new Thread(new Runnable() {
public void run() {
String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
}
}).start();
我不能用你的代码解决这个问题。我正在使用avd测试代码。可能是一些设置问题。因为蜂巢android在主UI线程上尝试联网时引发了异常。Oauth通过网络执行一些调用,但尚未更新以反映此错误。如果这是原因,那么在android 2.3.3上进行仿真时,您的代码不应产生此错误。此外,您还可以在ErrorStackTrace中搜索,它将在主线程上声明网络错误。我认为你使用的是android amd的最新版本之一,在UI线程上运行,对吗?我不是在UI线程中运行,是的,我使用的是最新版本的android。