2012-04-23 44 views
0

我是android新手我正在從android應用程序共享twitter鳴叫的android應用程序中工作,並且出現錯誤retrieveRequestToken引發「與服務提供者的通信失敗:null」。我從棧溢出流得到了這個解決方案(http://stackoverflow.com/questions/8534831/method-retrieverequesttoken-raises-communication-with-the-service-provider-fail)。它說我們將能夠通過StrictMode解決這個問題。我不知道如何應用這個。如何在android中應用StrictMode

這裏是馬代碼:

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+""); 
     } 

請幫助我。 在此先感謝。

回答

1

試試吧 -

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

由Suvan羅伊提供的答案將會有助於你在那一個方法花費太多時間(UI線程)的控制檯中看到的,但不是爲什麼它不工作。

我的建議:

  • 激活StricMode但只有在調試模式,而不是爲谷歌遊戲版本,並在控制檯日誌「嚴格模式」的錯誤經常檢查

在應用程序中執行此操作類(如果你有)和你的活動

@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 
    } 

爲了更好地利用資源,我建議還打電話給網絡提供商通過處理程序或AsynchTask。看一個例子你的代碼:

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

      } 
}); 

我希望你創辦你的問題的根源,你利用我的建議;)

相關問題