Facebook在Android Webview中发表评论

时间:2015-05-07 20:36:49

标签: android facebook webview

我正在尝试将带有Facebook评论部分的网页加载到我的Android WebView中。我按照这个问题提供的解决方案:Android unable to implement facebook comment in a webview due to default browser

当用户点击网页中的按钮登录Facebook时,会显示登录屏幕并成功登录,然后该视图关闭,并再次显示原始网页。但是,它们仍未登录。原始WebView处于与以前相同的状态,登录按钮仍在此处。实际上从childView登录到原始评论部分我错过了什么?相关代码粘贴在下面:

webView.setLayoutParams(getLayoutParams());

    webView.setWebChromeClient(new DollyChromeClient());

    webView.setWebViewClient(new DollyWebViewClient());

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setDisplayZoomControls(true);
    webView.getSettings().setBuiltInZoomControls(true);

    CookieManager.getInstance().setAcceptCookie(true);

private RelativeLayout.LayoutParams getLayoutParams(){
    return new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}



final class DollyChromeClient extends WebChromeClient {
    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        childView = new WebView(mContext);
        childView.getSettings().setDomStorageEnabled(true);
        childView.getSettings().setJavaScriptEnabled(true);
        childView.getSettings().setSupportZoom(true);
        childView.getSettings().setBuiltInZoomControls(true);
        childView.setWebViewClient(new FaceBookClient());
        childView.setWebChromeClient(this);
        childView.setLayoutParams(getLayoutParams());

        childView.getSettings().setSupportMultipleWindows(true);
        parentLayout.addView(childView);
        childView.requestFocus();
        webView.setVisibility(View.GONE);

        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();
        return true;
    }

    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        Log.d("WebViewDebug", cm.message() + " -- From line "
                + cm.lineNumber() + " of "
                + cm.sourceId());
        return true;
    }

    @Override
    public void onCloseWindow(WebView window) {
        parentLayout.removeViewAt(parentLayout.getChildCount() -1);
        childView = null;
        webView.setVisibility(View.VISIBLE);
        webView.requestFocus();
    }
}

private class FaceBookClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i("REQUEST URL", url);
        return false;
    }
}

2 个答案:

答案 0 :(得分:15)

经过几天的拔毛,结果是由Android Lollipop的变化引起的。它最终在旧设备上进行测试以实现问题的根源,这很快促成了我:https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView

终极解决方案原来是以下代码片段:

if (Build.VERSION.SDK_INT >= 21) {
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}

我希望这可以帮助将来的人减少比上周更少的头发。

答案 1 :(得分:1)

我有一个webview的例子,可以在这里使用facebook评论,也许它很有用...... https://gist.github.com/nguyenkims/28f3dc9d2414c3184263fab52b93351e