Webview不会完全加载wordpress页面

时间:2018-12-23 01:20:56

标签: android wordpress plugins webview

我需要在使用android webview的android应用中加载wordpress页面。我正在为该项目使用Android Studio。

插件:https://wordpress.org/plugins/wc-frontend-manager/

页面具有以下简短代码:[wc_frontend_manager]

Shortcode来自上述插件。

问题是,即使页面已正确加载到android webview中,也有其他重要信息(如客户,订单,通知等)未加载,因为它们是在页面加载后到达的... >

这就是为什么我认为问题是android webview不允许加载的jquery或ajax。

在移动或桌面浏览器中,页面均没有问题。

问题出在android webview中。

非常感谢您的宝贵时间。

我每天要花很多时间尝试很多天,但是我无法使其正常工作。

我认为这是jquery(ajax)的问题。

WebView webView;
String pageId, pageURL;
String pageTitle;
SettingsMain settingsMain;
RestService restService;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        pageId = bundle.getString("id", "");
        pageURL = bundle.getString("page_url", "");
        pageTitle = bundle.getString("pageTitle", "");
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_catgory_detaillist, container, false);
    settingsMain = new SettingsMain(getActivity());
    if (settingsMain.getAppOpen()) {
        restService = UrlController.createService(RestService.class);
    } else
        restService = UrlController.createService(RestService.class, settingsMain.getUserEmail(), settingsMain.getUserPassword(), getActivity());

    webView = view.findViewById(R.id.webView);
    webView.setScrollContainer(false);
    SwipeRefreshLayout swipeRefreshLayout = getActivity().findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setEnabled(false);

    if (pageURL.isEmpty()) {
        adforest_initialize();
    } else {
        adforest_getData(pageURL);
    }
    return view;
}

private void adforest_initialize() {
    if (SettingsMain.isConnectingToInternet(getActivity())) {

        SettingsMain.showDilog(getActivity());

        JsonObject params = new JsonObject();
        params.addProperty("page_id", pageId);
        Log.d("info Send CustomPages =", "" + params.toString());

        Call<ResponseBody> myCall = restService.postGetCustomePages(params, UrlController.AddHeaders(getActivity()));
        myCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> responseObj) {
                try {
                    if (responseObj.isSuccessful()) {
                        Log.d("info CustomPages Resp", "" + responseObj.toString());

                        JSONObject response = new JSONObject(responseObj.body().string());
                        if (response.getBoolean("success")) {
                            Log.d("info CustomPages object", "" + response.getJSONObject("data"));
                            pageTitle = response.getJSONObject("data").getString("page_title");
                            setScreenName(pageTitle);
                            getActivity().setTitle(pageTitle);
                            webView.loadData(response.getJSONObject("data").getString("page_content"), "text/html", "UTF-8");

                        } else {
                            Toast.makeText(getActivity(), response.get("message").toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    SettingsMain.hideDilog();
                } catch (JSONException e) {
                    SettingsMain.hideDilog();
                    e.printStackTrace();
                } catch (IOException e) {
                    SettingsMain.hideDilog();
                    e.printStackTrace();
                }
                SettingsMain.hideDilog();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                if (t instanceof TimeoutException) {
                    Toast.makeText(getActivity(), settingsMain.getAlertDialogMessage("internetMessage"), Toast.LENGTH_SHORT).show();
                    SettingsMain.hideDilog();
                }
                if (t instanceof SocketTimeoutException || t instanceof NullPointerException) {

                    Toast.makeText(getActivity(), settingsMain.getAlertDialogMessage("internetMessage"), Toast.LENGTH_SHORT).show();
                    SettingsMain.hideDilog();
                }
                if (t instanceof NullPointerException || t instanceof UnknownError || t instanceof NumberFormatException) {
                    Log.d("info CustomPages ", "NullPointert Exception" + t.getLocalizedMessage());
                    SettingsMain.hideDilog();
                } else {
                    SettingsMain.hideDilog();
                    Log.d("info CustomPages err", String.valueOf(t));
                    Log.d("info CustomPages err", String.valueOf(t.getMessage() + t.getCause() + t.fillInStackTrace()));
                }
            }
        });
    } else {
        SettingsMain.hideDilog();
        Toast.makeText(getActivity(), "Internet error", Toast.LENGTH_SHORT).show();
    }

}

public void replaceFragment(Fragment someFragment, String tag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.right_enter, R.anim.left_out, R.anim.left_enter, R.anim.right_out);
    transaction.replace(R.id.frameContainer, someFragment, tag);
    transaction.addToBackStack(tag);
    transaction.commit();
}

public void setScreenName(String pageTitle) {
    try {
        if (settingsMain.getAnalyticsShow() && !settingsMain.getAnalyticsId().equals(""))
            AnalyticsTrackers.getInstance().trackScreenView(pageTitle);
        super.onResume();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

}

@SuppressLint("SetJavaScriptEnabled")
private void adforest_getData(String url) {

    webView.getSettings().setJavaScriptEnabled(true); // enable javascript
    webView.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getContext(), description, Toast.LENGTH_SHORT).show();
        }

        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }
    });

    final Map<String, String> map = new HashMap<>();
    map.put("Adforest-Shop-Request", "body");
    if (settingsMain.getAppOpen()) {
        webView.loadUrl(url, map);
    } else {
        String authToken = Credentials.basic(settingsMain.getUserEmail(), settingsMain.getUserPassword());
        map.put("Authorization", authToken);
        if (SettingsMain.isSocial(getContext())) {
            map.put("AdForest-Login-Type", "social");
        }
        webView.loadUrl(url, map);
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url, map);

            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            SettingsMain.showDilog(getContext());
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            SettingsMain.hideDilog();
        }
    });
}

}

在android webview中加载页面后,还有其他数据需要加载,但目前还没有。

非常感谢您的宝贵时间。

0 个答案:

没有答案