gpt4 book ai didi

android - Android Webview-HTTP错误504网关超时

转载 作者:行者123 更新时间:2023-12-03 08:00:52 48 4
gpt4 key购买 nike

我有一个使用webview来显示Google App Engine网络应用程序的android应用程序。如何覆盖我的应用程序中遇到的默认HTTP Error 504 Gateway timeout

HTTP Error 504 Gateway timeout

The server, while acting as a gateway or proxy, did not receive a
timely response from the upstream server it accessed in attempting
to complete the request.

我已经覆盖了 onReceivedError,它在没有可用的互联网连接和其他错误时可以工作。
webView.setWebViewClient(new WebViewClient(){
...

@SuppressLint("DefaultLocale")
@Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {

try {
String template = streamToString(getAssets().open("html/error.html"));
String data = template.replaceAll("%DESCRIPTION%", description.toLowerCase())
.replaceAll("%WEBSITE_URL%", WEBSITE_URL);

view.loadDataWithBaseURL("file:///android_asset/html/", data, "text/html", "utf-8", null);
} catch (IOException e) {
e.printStackTrace();
}
}

});
onReceivedError仅能收到网络错误吗?任何解决方法? Android WebView如何拦截HTTP错误?

最佳答案

实际上,由于连接性低或与网络相关的原因,您的请求已超时;您可以通过以下方式显示自定义页面:

  webview.setWebViewClient(new WebViewClient() {
/* (non-Javadoc)
* @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
System.out.println("page loading started");
// TODO Auto-generated method stub
if(!isNetworkAvailable2())
{
//showInfoMessageDialog("network not available");
//load here your custom offline page for handling such errors

System.out.println("network not available");
return;
}
else System.out.println("network available");

super.onPageStarted(view, url, favicon);

}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
isConnected=isNetworkAvailable2();
if (isConnected) {
// return false to let the WebView handle the URL
return false;
} else {
// show the proper "not connected" message
// view.loadData(offlineMessageHtml, "text/html", "utf-8");
// return true if the host application wants to leave the current
// WebView and handle the url itself
return true;
}
}
@Override
public void onReceivedError (WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == ERROR_TIMEOUT) {
view.stopLoading(); // may not be needed
// view.loadData(timeoutMessageHtml, "text/html", "utf-8");
}
}
});
//webview.setWebChromeClient(new WebChromeClient());
}

Credits to the original answer here.

关于android - Android Webview-HTTP错误504网关超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16601115/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com