2012-05-11 71 views
4

enter image description hereTwitter的登錄對話框錯誤

您好我有集成的Twitter在我的Android application.here的問題是,當我嘗試登錄到它應該顯示在第一次登錄對話框Twitter的,它顯示了對話框正確,我可以登錄到Twitter帳戶,但在第二次嘗試登錄時顯示一個空白的twitter對話框。下面是我的代碼TwitterDialog calss。我該如何解決這個問題。請給我一個解決方案。

public class TwitterDialog extends Dialog { 

    static final float[] DIMENSIONS_LANDSCAPE = { 460, 260 }; 
    static final float[] DIMENSIONS_PORTRAIT = { 280, 420 }; 
    static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT); 
    static final int MARGIN = 4; 
    static final int PADDING = 2; 

    private String mUrl; 
    private TwDialogListener mListener; 
    private ProgressDialog mSpinner; 
    private WebView mWebView; 
    private LinearLayout mContent; 
    private TextView mTitle; 

    private static final String TAG = "Twitter-WebView"; 

    public TwitterDialog(Context context, String url, TwDialogListener listener) { 
     super(context); 
     mUrl = url; 
     mListener = listener; 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mSpinner = new ProgressDialog(getContext()); 
     mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     mSpinner.setMessage("Loading..."); 
     mContent = new LinearLayout(getContext()); 
     mContent.setOrientation(LinearLayout.VERTICAL); 
     setUpTitle(); 
     setUpWebView(); 
     Display display = getWindow().getWindowManager().getDefaultDisplay(); 
     final float scale = getContext().getResources().getDisplayMetrics().density; 
     float[] dimensions = (display.getWidth() < display.getHeight()) ? DIMENSIONS_PORTRAIT: DIMENSIONS_LANDSCAPE;addContentView(mContent, new FrameLayout.LayoutParams((int) (dimensions[0] * scale + 0.5f), (int) (dimensions[1]* scale + 0.5f))); 
    } 

    private void setUpTitle() { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     Drawable icon = getContext().getResources().getDrawable(R.drawable.twitter_icon); 
     mTitle = new TextView(getContext()); 

     mTitle.setText("Twitter"); 
     mTitle.setTextColor(Color.WHITE); 
     mTitle.setTypeface(Typeface.DEFAULT_BOLD); 
     mTitle.setBackgroundColor(0xFFbbd7e9); 
     mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN); 
     mTitle.setCompoundDrawablePadding(MARGIN + PADDING); 
     mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 
     mContent.addView(mTitle); 
    } 

    private void setUpWebView() { 
     mWebView = new WebView(getContext()); 

     mWebView.setVerticalScrollBarEnabled(false); 
     mWebView.setHorizontalScrollBarEnabled(false); 
     mWebView.setWebViewClient(new TwitterWebViewClient()); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.loadUrl(mUrl); 
     mWebView.setLayoutParams(FILL); 
     //mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
     mContent.addView(mWebView); 
    } 

    private class TwitterWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Constants.LOG)Log.d(TAG, "Redirecting URL " + url); 
      if (url.startsWith(TwitterApp.CALLBACK_URL)) { 
       mListener.onComplete(url); 
       //mWebView.getSettings().setAppCacheEnabled(false); 
       //mWebView.refreshDrawableState(); 
       if (Constants.LOG)Log.d(TAG, "Clearing chache"); 
       TwitterDialog.this.dismiss(); 
       return true; 
      } else if (url.startsWith("authorize")) { 
       return false; 
      } 
      return true; 
     } 
     @Override 
     public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) { 
      if (Constants.LOG)Log.d(TAG, "Page error: " + description); 
      super.onReceivedError(view, errorCode, description, failingUrl); 
      mListener.onError(description); 
      TwitterDialog.this.dismiss(); 
     } 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      if(Constants.LOG)Log.d(TAG, "Loading URL: " + url); 
      super.onPageStarted(view, url, favicon); 
      mSpinner.show(); 
     } 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      String title = mWebView.getTitle(); 
      if (title != null && title.length() > 0) { 
      mTitle.setText(title); 
      } 
      mSpinner.dismiss(); 
     } 

    } 
}`` 

回答

6

您需要強制登錄第二次來解決這個問題,

所以只需添加force_login =真在你的Twitter授權網址。

我的意思只是改變了下面的代碼行中TwitterApp.java類(如果你有不同名稱的類文件,然後在您的項目與搜索DefaultOAuthProvider

mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", 
       "http://twitter.com/oauth/access_token", 
     "http://twitter.com/oauth/authorize?force_login=true"); 

加入後force_login = true webview加載問題將被解決,但每次您需要輸入登錄名和密碼。

我被卡住整天解決這個問題。

+0

很好的答案,它的工作感謝Vivek ......... – Ganesh

+0

非常感謝您對本文的評論對於大多數使用Twitter的開發者來說,這真的是一個非常大的問題.....我有一個問題。如果我已經登錄,我的訪問令牌是有效的,但仍然想顯示登錄對話框是可能的。如果登錄後它是這樣的話,它會生成一個新的訪問令牌,或者它將使用舊的有效訪問令牌?新登錄後 –

+1

它可能會返回相同或不同的access_token。所以你需要每次更新你的access_token。 –