AsyncTask和goBack

时间:2013-09-04 18:44:50

标签: java android android-asynctask jsoup

任何人都可以告诉我,为什么Back按钮返回黑页?我在AsyncTask中使用Jsoup解析。看起来历史不存在。我应该在代码中修改什么来修复它?

    WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webView1);
        webview.setWebViewClient(new ForumWebViewClient());
        String url = "https://forum.com";
        new ParseMyPageTask().execute(url);
    }
    private class ForumWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView webview, String url) {
            new ParseMyPageTask().execute(url);
            return true;
        }
    }
    class ParseMyPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            Document doc = null;
            String result = "";
            try {
                doc = Jsoup.connect(params[0]).get();
                Elements body = doc.select("body");
                result = body.html();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            webview.getSettings().setUseWideViewPort(true);
            webview.setInitialScale(0);
            webview.loadDataWithBaseURL("https://forum.com/",
                    "<head><meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" /><link rel=\"stylesheet\" type=\"text/css\" href=\"/data/styles/black.css\"></head><body>" + result + "</body>", "text/html", "UTF-8", "");
        }
    }   
    public void onBackPressed() {
        if (webview.canGoBack()) {
            webview.goBack();
            return;
        }
        super.onBackPressed();
    }

我真的需要你的帮助!

1 个答案:

答案 0 :(得分:0)

您需要加载loadDataWithBaseURL并提供您为历史记录加载的相同URL! 目前正在尝试自己如何实现GOBACK,以便以相同的方式解析它!

Visits are welcome