天天看点

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))      

继续阅读