Authorization with jTwitter android

413 views Asked by At

Am a newbie to twitter api's and async tasks. I was trying OAuth using jTwitter which seems to be simple API. Since you cannot do network operations on main thread. I created an asynctask to the work. It is throwing me exceptions which i dont understand. Kindly help me resolving the exceptions.

Here is the code

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

and the exceptions which i get

  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)
1

There are 1 answers

2
Siddharth Lele On

The doInBackground() method is not meant for performing any UI tasks. You should use the onPostExecute() for that. If you need to show the Toast in the doInBackground(), put it in a Runnable() as illustrated in the code.

Runnable run = new Runnable() {

    @Override
    public void run() {

        // SHOW A TOAST
        Toast.makeText(getApplicationContext(), "background stuff", Toast.LENGTH_LONG).show();                          
    }
}; MainActivity.this.runOnUiThread(run);