android webview机制,Android webview 处理 500 的机制,目前发现不起作用

网上大部分找到的是overstack如下方法,自己模拟一个httpClient

对httpget惊醒请求,经测试,不可用,不能拦截500的错误。stat = one 就打印不出。

@Override

public boolean shouldOverrideUrlLoading(WebView view, String

urlConection) {

ToastUtil.popupToast(getApplicationContext(), "stat =

one");

URL url;

URLConnection conexion;

try {

url = new URL(urlConection);

conexion = url.openConnection();

conexion.setConnectTimeout(3000);

conexion.connect();

// get the size of the file which is in the header of

the

// request

int size = conexion.getContentLength();

} catch (Exception e) {

e.printStackTrace();

ToastUtil.popupToast(getApplicationContext(), "stat =

two");

}

// and here, if you want, you can load the page normally

String htmlContent = "";

HttpGet httpGet = new HttpGet(urlConection);

// this receives the response

HttpResponse response;

ToastUtil.popupToast(getApplicationContext(), "stat =

start");

try {

HttpClient httpClient = new DefaultHttpClient();

response = httpClient.execute(httpGet);

ToastUtil.popupToast(getApplicationContext(), "stat = " +

response.getStatusLine().getStatusCode());

if (response.getStatusLine().getStatusCode() == 200) {

// la conexion fue establecida, obtener el contenido

HttpEntity entity = response.getEntity();

if (entity != null) {

InputStream inputStream = entity.getContent();

htmlContent = convertToString(inputStream);

}

} else {

// 返回码 非200,均提示小哭脸

pageLoadSuccess = false;

}

ToastUtil.popupToast(getApplicationContext(), "stat = " +

response.getStatusLine().getStatusCode());

} catch (Exception e) {

e.printStackTrace();

}

view.loadData(htmlContent, "text/html", "utf-8");

return true;

}

public String convertToString(InputStream inputStream) {

StringBuffer string = new StringBuffer();

BufferedReader reader = new BufferedReader(new

InputStreamReader(inputStream));

String line;

try {

while ((line = reader.readLine()) != null) {

string.append(line + "\n");

}

} catch (IOException e) {

}

return string.toString();

}

---------------------------------------------------------

目前我能解决的就是处理404等一些浏览器器解析的错误,像类似500这种服务端的响应

,实际上已经是httpget正确,但是服务server方解析或者处理异常(空指针等情况),目前还没找到webview

对这方面的回调相应。

对应由浏览器相应的解决想法如下,这也是自己琢磨出来的,他会自己去起一个Android

assert下的默认错误页面。

@Override

public void onLoadResource(WebView view, String url) {

if (url != null &&

url.equals("file:///android_asset/webkit/android-weberror.png"))

{

ll_no_data.setVisibility(View.VISIBLE);

pageLoadSuccess = false;

} else {

ll_no_data.setVisibility(View.GONE);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值