0

I am new to android I am working in an android application that share twitter tweets from android application and I am getting an error retrieveRequestToken raises “Communication with the service provider failed: null”. I got a solution for this from stack over flow(http://stackoverflow.com/questions/8534831/method-retrieverequesttoken-raises-communication-with-the-service-provider-fail). It says that we will be able to solve this problem by StrictMode . I don't know how to apply this.

Here is ma code :

try {

            Log.i(TAG, "Retrieving request token from Google servers");
            final String url = provider.retrieveRequestToken(consumer,Constants.OAUTH_CALLBACK_URL);                                                
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
                    .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                            | Intent.FLAG_ACTIVITY_NO_HISTORY
                            | Intent.FLAG_FROM_BACKGROUND);         
            context.startActivity(intent);



        } catch (Exception e) {

            Log.e("Error",e+"");
        }

Please help me. Thanks in advance.

BonDaviD
  • 961
  • 3
  • 11
  • 27

2 Answers2

1

Try it -

private void enableStrictMode(Bundle savedInstanceState) {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                                             .detectDiskReads()
                                             .detectDiskWrites()
                                             .detectNetwork()
                                             .penaltyLog()
                                             .build());

    super.onCreate(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    enableStrictMode(savedInstanceState);
    setContentView(layout.main);
}
Suvam Roy
  • 1,282
  • 2
  • 11
  • 21
0

the answer provided by Suvan Roy it will help you see in the console that one method take too much time (in UI Thread), but not why it doesn't work.

My suggestion:

  • Activate StricMode but only in DEBUG mode and not for Google Play releases and check always in the console log for "strict mode" errors

Do this in your application class (if you have one) and your activities

    @Override 
    public void onCreate(Bundle savedInstanceState) {
       if (BuildConf.DEBUG){
             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                                             .detectDiskReads()
                                             .detectDiskWrites()
                                             .detectNetwork()
                                             .penaltyLog()
                                             .build());
    }
    super.onCreate(savedInstanceState);
       // your code here
    }

In order to do a better use of resources I suggest also to call the network provider through a Handler or AsynchTask. See an example with your code:

new Handler().post( new Runnable()
{
           @Override
           public void run()
           {
              Log.i(TAG, "Retrieving request token from Google servers");
              final String url = provider.retrieveRequestToken(consumer,Constants.OAUTH_CALLBACK_URL);                                                
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
                      .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                              | Intent.FLAG_ACTIVITY_NO_HISTORY
                              | Intent.FLAG_FROM_BACKGROUND);         
              context.startActivity(intent);

           }
} );

I hope you founded the root of your problem and you make use of my suggestions ;)

Tano
  • 3,620
  • 1
  • 29
  • 31