是这样的,我发现myWebView.loadUrl(address);方法执行之后,有时候会把里面的参数address转换成一个奇怪的乱码,于是我想让它在shouldoverrideurlloading中先检测一下:(请看里面的注释)
myWebView.loadUrl(address);
myWebView.setWebViewClient(new myWebViewClient());
class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//我传进loadUrl中的参数是:http://www.zhihu.com/question/45968097/answer/100778963
//但不知为何,有时候传进来的url到了这一步打印,会变成下面这样:
System.out.println(url);
//http://www.zhihu.com/?next=%2Fquestion%2F45968097%2Fanswer%2F100778963
//于是我想把上面的url改成原本正常的样式,进行下面的字符串替换:
if(url.contains("?next="))
{
url = url.replace("%2F", "/");
url = url.replace("?next=/","");
System.out.println("replace result--->"+url);
}
//但这样做之后replace result--->输出的url是正常了,但会一直循环执行,一致打印这一句replace result--->,页面也打不开,一直卡在那儿。请问这是什么原因?
view.loadUrl(url);
return false;
}
为什么会循环执行?怎么解决呢..或者能不能告诉我为什么url会变成那种乱码。。
我想了一下,之所以会循环执行,是因为shouldOverrideUrlLoading中的 view.loadUrl(url);又把url变成错误的形式 //http://www.zhihu.com/?next=%2Fquestion%2F45968097%2Fanswer%2F100778963
了。所以为什么url会这样变化呢?这是不是android loadUrl算法的故障?
If you write the address correctly, there will be no problem in using it. If it doesn’t work, just try another version of your phone
The "garbled code" you see is not garbled code, but urlencode encoding. "%2F" is "/", and the loop you are talking about is actually not a loop, but a 302 page running, which is what you see. The address reached is not pointed directly, but is redirected to the final target page by server routing, and the page jump can be monitored by the shouldOverrideUrlLoading method, so it feels like a loop. Regarding the 302 page and server-side routing, you can learn more about it.