4

在此处输入图像描述

嗨,我已经在我的 android 应用程序中集成了 twitter。这里的问题是,当我尝试登录 twitter 时,它应该第一次显示登录对话框,它正在正确显示对话框并且我能够登录到 twitter 帐户,但是在我第二次尝试登录时,它显示一个空白的 Twitter 对话框。下面是我的 TwitterDialog 代码。我该如何解决这个问题。请给我一个解决方案。

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

    }
}``
4

1 回答 1

6

You need to force login second time to resolve this issue,

So just add the force_login=true in your twitter authorize url.

I mean just change the below line of code in TwitterApp.java class (if you have a class file with different name then search in your project with DefaultOAuthProvider)

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

after adding force_login=true webview loading issue will be resolve but every time you need to enter login and password.

I was stuck whole day to resolve this issue.

于 2012-05-11T09:11:27.437 回答