Facebook对Android webview的评论:对象缺少必需的值

时间:2016-06-23 05:23:44

标签: facebook-comments

我正在开发的社交项目ANROID平台。 我正在尝试将一个网页加载到Facebook评论部分到我的Android WebView。 目前我有一个问题,当我第一次发表评论 时,会显示消息“对象缺少必需的值”。 我的代码如下:

private void InitLoadComment(){
    webViewFB = new WebView(this);
    webViewFB.setWebViewClient(new FaceBookClient());
    webViewFB.setWebChromeClient(new FBChromeClient());
    webViewFB.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    WebSettings webSettings = webViewFB.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    if (Build.VERSION.SDK_INT >= 21) {
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        CookieManager.getInstance().setAcceptThirdPartyCookies(webViewFB, true);
    }

    String newsURL = "http://example_test.com/thefirsttime.php";
    parentLayout.addView(webViewFB);
    webViewFB.loadDataWithBaseURL("http://example_test.com", getHTMLData( newsURL, title ), "text/html", null, "");
}

private String getHTMLData(String newsURL , String title) {

    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head>");
    sb.append("<meta property=\"fb:app_id\"      content=\"MY FACEBOOK APP ID\" />");
    sb.append("</head>");
    sb.append("<body>");
    sb.append("<h2>"+ title +"</h2>");
    sb.append("<div class=\"fb-comments\" data-href=\"" + newsURL + "\" data-num-posts=\"5\" data-width=\"680\"></div>");
    sb.append("<div id=\"fb-root\"></div>");
    sb.append("<script>(function(d, s, id) {");
    sb.append("var js, fjs = d.getElementsByTagName(s)[0];");
    sb.append("if (d.getElementById(id)) {return;}");
    sb.append("js = d.createElement(s); js.id = id;");
    sb.append("js.src = \"//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6&appId=MY FACEBOOK APP ID\";");
    sb.append("fjs.parentNode.insertBefore(js, fjs);");
    sb.append("}(document, 'script', 'facebook-jssdk'));");
    sb.append("</script>");
    sb.append("</body>");
    sb.append("</html>");
    return sb.toString();
}

private class FaceBookClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String host = Uri.parse(url).getHost();
        LogHelper.d(TAG,"REQUEST URL : "+ host);
        if (host.contains("facebook.com")) {
            view.loadUrl(url);
            return true;
        }
        // Otherwise, the link is not for a page on my site, so launch
        // another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return false;
    }
}

final class FBChromeClient extends WebChromeClient {

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        childWebView = new WebView(PlayActivity.this);
        childWebView.getSettings().setJavaScriptEnabled(true);
        childWebView.getSettings().setSupportZoom(true);
        childWebView.getSettings().setBuiltInZoomControls(true);
        childWebView.setWebViewClient(new FaceBookClient());
        childWebView.setWebChromeClient(this);
        childWebView.setLayoutParams(PlayActivity.this.getLayoutParams());

        parentLayout.addView(childWebView);

        childWebView.requestFocus();
        webViewFB.setVisibility(View.GONE);

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

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

}

注意:此问题不会在第二次发生。

您能否帮助或给我一些建议来解决这个问题。

谢谢, 白德曼

0 个答案:

没有答案