我正在使用 jTwitter for android 开发我的应用程序。我能够将用户重定向到授权页面,他在其中输入凭据以授权应用程序。然后调用回调函数。在回调函数中无法获取访问令牌。它正在向我抛出异常。请帮忙。
我收到“与服务提供商的通信失败”异常。更新:我得到了令牌,但仍然得到了例外。这是代码:
Twitter twitter = null;
private EditText tweetTxt = null;
private Button postBtn = null;
private OAuthSignpostClient client = null;
private String CALLBACK_URI = "myapp://twitt";
private String authUrl = null;
private ProgressDialog postDialog = null;
String[] accessTokenandSecret=null;
String consumerKey="";
String consumerSecret="";
String verifier;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postBtn=(Button)findViewById(R.id.button1);
postBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute();
}
});
Button postTweet = (Button)findViewById(R.id.button2);
postTweet.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new PostTweet().execute();
}
});
}
private class PostTweet extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute 2", Toast.LENGTH_LONG).show();
if(accessTokenandSecret==null)
{
Toast.makeText(getApplicationContext(), "IT IS NULL", Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(Void... arg0) {
client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret[0], accessTokenandSecret[1]);
Twitter jtwit = new Twitter(null, client);
jtwit.setStatus("test post from my app");
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "Tweet is post Successfully!!", Toast.LENGTH_SHORT).show();
}
}
private class MyTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
client = new OAuthSignpostClient("QvkiReKCHNKcHn3pGrEzQ" ,"EV9vdMRfuT2AQlSNPJW4LhDAyOe0z1mAZYqHJNkPH7g", CALLBACK_URI);
authUrl = client.authorizeUrl().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "post execute", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
//Check if you got NewIntent event due to Twitter Call back only
if (uri != null && uri.toString().startsWith(CALLBACK_URI))
{
try
{
verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Toast.makeText(getApplicationContext(), "inside verfier", Toast.LENGTH_LONG).show();
client.setAuthorizationCode(verifier);
accessTokenandSecret=client.getAccessToken();
//if(accessTokenandSecret==null)
//{
//Toast.makeText(getApplicationContext(), "it is null you fool", Toast.LENGTH_LONG).show();
// }
//client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret);
}
catch(Exception e){
Log.d("exception", " "+e.getMessage());
}
}}
日志猫错误:
02-17 15:13:46.599: E/AndroidRuntime(12915): FATAL EXCEPTION: AsyncTask #4
02-17 15:13:46.599: E/AndroidRuntime(12915): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 15:13:46.599: E/AndroidRuntime(12915): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 15:13:46.599: E/AndroidRuntime(12915): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.lang.Thread.run(Thread.java:856)
02-17 15:13:46.599: E/AndroidRuntime(12915): Caused by: winterwell.jtwitter.TwitterException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: http://twitter.com/oauth/access_token
02-17 15:13:46.599: E/AndroidRuntime(12915): at winterwell.jtwitter.OAuthSignpostClient.setAuthorizationCode(OAuthSignpostClient.java:431)
02-17 15:13:46.599: E/AndroidRuntime(12915): at com.example.retrieve.contacts.twitter.MainActivity$GetAccessTok.doInBackground(MainActivity.java:114)