0

我陷入了以下问题!我解析网站,这是我的大概代码:

public class MainActivity extends Activity {
String CurURL = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
    webview.setWebViewClient(new ForumWebViewClient());
    new ParseMyPageTask().execute(url);
    }
    private class ForumWebViewClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        new ParseMyPageTask().execute(url);
        return true;
    }
    }

    public class ParseMyPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
                    *JSoup work*
        return result;
            }
    @Override
    protected void onPostExecute(String result) {
            webview.loadDataWithBaseURL("http:..", result, "text/html", "UTF-8", CurURL);
    }
    }
    @Override
public void onBackPressed() {
    if (webview.canGoBack()) {
        webview.goBack();
        return;
    }
    super.onBackPressed();
}
}

按下后退按钮后,我应该为 AsyncTask 执行 url 做什么?现在它按原样返回页面,未解析。

4

1 回答 1

1

您需要操纵WebBackForwardList来加载您的 URL,而不是默认情况下由 WebView 自动缓存的那些。

您可以通过覆盖来做到这一点WebView.goBack()

  @Override
  public void goBack() {
    WebBackForwardList temp = copyBackForwardList();
    for (int i=0; i<temp.getSize(); i++) {
      WebHistoryItem item = temp.getItemAtIndex(i);
      // do whatever you want here...
    }

    // do whatever you want here...
    super.goBack();
  }
于 2013-10-05T21:53:54.513 回答