我是 twitter api 和异步任务的新手。我正在尝试使用 jTwitter 的 OAuth,这似乎是简单的 API。由于您不能在主线程上进行网络操作。我为这项工作创建了一个异步任务。它向我抛出了我不理解的异常。请帮助我解决异常。
这是代码
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;
@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();
}
});
}
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("" ,"", CALLBACK_URI);
authUrl = client.authorizeUrl().toString();
getApplicationContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
@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
{
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
client.setAuthorizationCode(verifier);
}
catch(Exception e){
Log.d("exception", e.getMessage());
}
}}
和我得到的例外
02-17 10:58:54.751: E/AndroidRuntime(987): FATAL EXCEPTION: AsyncTask #1
02-17 10:58:54.751: E/AndroidRuntime(987): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 10:58:54.751: E/AndroidRuntime(987): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.lang.Thread.run(Thread.java:856)
02-17 10:58:54.751: E/AndroidRuntime(987): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-17 10:58:54.751: E/AndroidRuntime(987): at android.app.ContextImpl.startActivity(ContextImpl.java:886)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.app.ContextImpl.startActivity(ContextImpl.java:880)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-17 10:58:54.751: E/AndroidRuntime(987): at com.example.retrieve.contacts.twitter.MainActivity$MyTask.doInBackground(MainActivity.java:72)