3

I am trying to build an android app so that it tweets in twitter.
I am trying to get uri using the function retrieveRequestToken(consumer,CALLBACK_URI) but it throws 'OAuthCommunicationException' exception. I gave the internet uses permission for the application( i have checked it ).I have registerd the app in twitter giving read,write and direct messages.

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)

Here is the code snippet

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

            }
sayem siam
  • 1,281
  • 3
  • 13
  • 26

1 Answers1

3

This is caused by a network on main thread exception. Try running the oauth code on a thread:

          new Thread(new Runnable() {
                public void run() {
                    String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
                }
              }).start();
  • I could not solve the problem using ur code. I am testing the code using avd .Threre may be some settings problem. – sayem siam Apr 08 '12 at 17:36
  • Since HoneyComb android has thrown an exception when networking is attempted on the main UI thread. Oauth performs some calls over the network and has not been updated to reflect this error. If this is the reason, your code should not produce this error when emulated on android 2.3.3. Also you could search in the error stacktrace which will state the network on main thread error. Am I correct in thinking you are using one of the latest versions of android amd running this on the UI thread? – megatron-me-uk Apr 09 '12 at 00:45
  • I am not running this in UI Thread and yes i am using latest version of android. – sayem siam Apr 09 '12 at 08:18