shmakova shmakova - 3 months ago 25
Android Question

Android WebView - Infinite loop after click

I want to create simple child-safety browser on Android, and I want to check every url before load with this api: https://www.mywot.com/wiki/API. But sometimes after click on url starts infinite loop of load this link.
I try to override shouldOverrideUrlLoading like this:

private class BrowserWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
loadUrl(url);
return true;
}
}
}

private interface UrlCheckListener {
void urlChecked(boolean isGood);
}

private void loadUrl(String query) {
String safeUrl = UrlCheckerUtils.getSafeUrlFromQuery(query);
checkUrlBeforeLoad(safeUrl, valid -> {
if (isViewAttached()) {
if (valid) {
getView().loadPageByUrl(safeUrl);
} else {
getView().showError();
}
}
});
}

private void checkUrlBeforeLoad(String url, UrlCheckListener listener) {
try {
URI uri = new URI(url);
String domain = uri.getHost() + "/";

wotService.getLinkReputation(domain)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(wotResponse -> listener.urlChecked(wotResponse.isSafe()));
} catch (URISyntaxException e) {
Timber.e(e, "Error while parsing url");
}
}


loadPageByUrl in view:

@Override
public void loadPageByUrl(String url) {
webView.loadUrl(url);
}


Can you help me to fix this please?

Answer

The error was in String safeUrl = UrlCheckerUtils.getSafeUrlFromQuery(query); where I created new object of String. When I start manipulate with url in shouldOverrideUrlLoading, it starts work without infinite redirects.

Comments