通过WebView.goBack执行url

时间:2013-10-04 21:06:30

标签: java android android-asynctask android-webview jsoup

我在以下问题中堆叠!我解析网站,这是我的近似代码:

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?现在它按原样返回页面,而不是解析。

1 个答案:

答案 0 :(得分:1)

您需要操纵WebBackForwardList来加载您的网址,而不是默认情况下由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();
  }