2012-01-08 51 views
3

我想構建一個android應用程序,以便它在推文中推文。
我想使用函數retrieveRequestToken(consumer,CALLBACK_URI)獲取uri,但它會拋出'OAuthCommunicationException'異常。 我給了互聯網使用應用程序的權限(我已經檢查過)。我已經在twitter上註冊了應用程序,提供讀取,寫入和直接消息。everythings似乎確定,但函數retrieveRequestToken()拋出'OAuthCommunicationException'異常

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) 

這裏是代碼片段

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

      } 

回答

3

這是通過在主線程異常的網絡造成的。嘗試在一個線程上運行oauth代碼:

  new Thread(new Runnable() { 
       public void run() { 
        String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI); 
       } 
       }).start(); 
+0

我無法用ur代碼解決問題。我正在使用avd測試代碼.Threre可能是一些設置問題。 – 2012-04-08 17:36:01

+0

由於HoneyComb android在主UI線程上嘗試聯網時引發了異常。 Oauth通過網絡執行一些呼叫,但沒有更新以反映此錯誤。如果這是原因,在android 2.3.3上模擬時,你的代碼不應該產生這個錯誤。你也可以在錯誤堆棧跟蹤中搜索哪個主線程錯誤的網絡。我是否正確地認爲你使用的是在UI線程上運行它的最新版本的android amd? – 2012-04-09 00:45:53

+0

我沒有在UI線程中運行此操作,而且我正在使用最新版本的android。 – 2012-04-09 08:18:35