简体   繁体   中英

Execute url by WebView.goBack

I'm stack in following problem! I parse website, and here is my approximate code:

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();
}
}

What should i do for AsyncTask execute url after back button pressed? Now it return page as it is, not parsed.

You need to manipulate WebBackForwardList to load your URL, not the ones automatically cached by WebView by default.

You do that by overriding 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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM