2012-12-02 78 views
4

我正在嘗試從Android Twitter oAuth Connect Tutorial代碼,它的工作成功。我嘗試將Twitter授權頁面更改爲在WebView中運行,而不是Web瀏覽器,但WebView似乎無法加載此格式的url,這是鏈接回到我的應用程序的鏈接。成功授權後,webview應關閉併成功返回到我的應用程序。WebView無法加載oAuth網址

有一個錯誤:「oauth:// twittersample?oauth_token = ....的網頁可能暫時關閉,或者它可能永久移動到新的網址」。我該怎麼辦?

這是段我的WebView這是在我的onCreate

WebView myWebView = (WebView)findViewById(R.id.myWebView); 

    myWebView.setWebViewClient(new WebViewClient() 
    { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView webView, String url) 
    { 
    if (url != null && url.startsWith("oauth://twittersample")) 
     //handleTwitterCallback(url); 
    { 
     System.out.println("TWEET TWEET TWEET");  
     webView.loadUrl(url); 
     return true; 

     } 

    else 

    return false; 
    } 
    }); 

這是鏈接到我的Twitter的java類TWITTER CONNECT CLASS 這是我的清單

<activity android:name="com.test.settings.ShareSettings" android:label="ShareSettings" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden">  
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:scheme="oauth" android:host="twittersample"/> 
     </intent-filter> 
    </activity> 

附件爲logcat的在瀏覽器中成功運行時

enter image description here

+1

請不要在您的問題標題中添加「已解決」。選擇一個正確答案會自動顯示問題列表中的問題。 – hjpotter92

回答

4

我終於得到它的工作。我之前認爲它不起作用,因爲我沒有在WebView中檢索訪問令牌。

在我的onCreate下的WebView我這樣做

myWebView = (WebView)findViewById(R.id.myWebView); 
    myWebView.setWebViewClient(new WebViewClient() 
    { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView webView, String url) 
    { if (url != null && url.startsWith(TWITTER_CALLBACK_URL)) 

    { System.out.println("TWEET TWEET TWEET"); 
     retrieveAccessToken(url); //added this 
     webView.setVisibility(View.GONE); //added this 
     return true;    
     } 

    else 

    return false; 

    } 
    }); 

,並在我的retrieveAccessToken(URL)我有這個。

private void retrieveAccessToken(final String url) 
{ 
    Uri uri = Uri.parse(url); 
    String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER); 

    try { 
     // Get the access token 
     AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); 

     // Shared Preferences 
     Editor e = mSharedPreferences.edit(); 

     // After getting access token, access token secret 
     // store them in application preferences 
     e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken()); 
     e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret()); 
     // Store login status - true 
     e.putBoolean(PREF_KEY_TWITTER_LOGIN, true); 
     e.commit(); // save changes 

     Log.e("Twitter OAuth Token", "> " + accessToken.getToken()); 

     TextView twitterConnect = (TextView) findViewById(R.id.twitterConnect); 
     String disconnect = "Disconnect"; 
     twitterConnect.setText(disconnect);              

     // Getting user details from twitter 
     // For now i am getting his name only 
     long userID = accessToken.getUserId(); 
     User user = twitter.showUser(userID); 
     String username = user.getName(); 

     txtUpdate.setVisibility(View.VISIBLE); 
     btnUpdateStatus.setVisibility(View.VISIBLE); 

     // Displaying in xml ui 
     //twitterUser.setText(Html.fromHtml("<b>Welcome " + username + "</b>")); 
     TextView twitterUser = (TextView) findViewById(R.id.twitterDesc);  
     twitterUser.setText(Html.fromHtml(username)); 
     Toast.makeText(getApplicationContext(), "LOGGED IN AS " + username, Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     // Check log for login errors 
     Log.e("Twitter Login Error", "> " + e.getMessage()); 
    } 
} 

我得到這個工作正是我想要的,併成功登錄。 糾正我,如果我在這裏做錯什麼。

謝謝@ user1690588和@尼古拉伊倫科夫,您的時間來幫助我。 :)

0

我也有這個問題,我意識到我的回調URL不匹配我的清單文件中的過濾條目。也許你也有同樣的問題。

您可以通過以下步驟解決這個問題:

設置你的回調URL在你的類作爲

oauth://twittersample 

,並在您AndroidManifest

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <data android:scheme="oauth" android:host="twittersample"/> 
</intent-filter> 

試試吧..,。

確保您的回調URL

XXX://YYY 

匹配

<data android:scheme="XXX" android:host="YYY"/> 

編輯........................ .................................................. ........

嘗試this like也..,。

+0

是的,我的清單中也有這樣的內容,它與我在java課程中的內容相匹配,但我仍面臨同樣的問題。 –

1

使用HTTP網址,如http://localhost/twittersample/oauth_callback或類似網址。

+0

我試過了你的方法,但它沒有工作。我想知道我做錯了什麼。 –

+0

您是否更新清單中的意圖過濾器?你使用的是什麼版本的Android? –

+0

是的,我更新了清單中的intent過濾器,以匹配我的java類中的url。我正在使用Android 4.1.2。 –