0
点赞
收藏
分享

微信扫一扫

okhttp+retrofit常见网络异常收集


1.Canceled
这个发生在多次获取同一类型数据,取消前一次请求的情况
2.Socket closed
网络连接过长,或断断续续,获取的socket是关闭状态(取消前一次请求也可能会造成这个)
3.field null map
这个是参数有FieldMap然后map是null
4.Unable to resolve host:xxx,no address associated with host name。
这个出现在把wifi关闭的情况,是信号不好的一种情况。
5.ProtocolException:unexpected end of stream
目前查到几个发生这个问题的原因:1.下载2.网络差3.content-length数目比实际传送数据要小。
找的这篇文和我自身遇到的问题差不多,也是同时传链接和图片时失败。
为了防止链接失效,记录一下:

public static byte[] file2Byte(File file){
byte[] buffer = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[]b = new byte[1024];
int n;
while((n=fis.read(b))!=-1){
bos.write(b,0,n);
}
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return

调用:

RequestBody.create(MediaType.parse("image/*"), file)

改为:

RequestBody.create(MediaType.parse("image/*"), file2Byte(file))

6.no internet connection
没有网络,信号不好的一种

面对花样作死版的网络异常,无奈暂时只能在发布版对无法预计的错误(原生错误)一律提示网络信息号不好,测试版正常抛出方便调试。服务器可以处理的抛出一种服务器错误信息(errorbody),返回code非200的显示错误码。


举报

相关推荐

0 条评论