okhttp 上传图片java.net.SocketTimeoutException: timeout
【摘要】 使用OKHttp上传图片时,发生的超时异常。推测主要是网络的原因,重新设置连接、读超时、及池化连接,将最大空闲数和等待时间设大。多,且线程池中核心线程数量设置为10,最大线程数量设置为24。调用OkHttpUtils.getInstance()获取okhttpClient对象,在并发运行时需保证足够数量的okhttp连接。原来的代码底层用的默认值:连接池最大连接空闲数5...
使用OKHttp上传图片时,发生的超时异常。推测主要是网络的原因,重新设置连接、读超时、及池化连接,将最大空闲数和等待时间设大。多,且线程池中核心线程数量设置为10,最大线程数量设置为24。调用OkHttpUtils.getInstance()获取okhttpClient对象,在并发运行时需保证足够数量的okhttp连接。原来的代码底层用的默认值:连接池最大连接空闲数5,连接超时10秒,读写超时15秒。我们重新定义okhttp连接池最大空闲数量设为32,防止无连接可用等待超时后抛异常
package com.wong.demo;
import android.text.TextUtils;
import com.csair.pda.Global;
import com.csair.pda.PdaApplication;
import java.util.concurrent.TimeUnit;
import okhttp3.Callback;
import okhttp3.ConnectionPool;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class OkHttpUtils { private final static int READ_TIMEOUT = 100; private final static int CONNECT_TIMEOUT = 60; private final static int WRITE_TIMEOUT = 60; private static volatile OkHttpClient okHttpClient; private OkHttpUtils(){ okhttp3.OkHttpClient.Builder clientBuilder = new okhttp3.OkHttpClient.Builder(); //读取超时 clientBuilder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS); //连接超时 clientBuilder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS); //写入超时 clientBuilder.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS); //自定义连接池最大空闲连接数和等待时间大小,否则默认最大5个空闲连接 clientBuilder.connectionPool(new ConnectionPool(32,5,TimeUnit.MINUTES)); okHttpClient = clientBuilder.build(); } public static OkHttpClient getInstance(){ if (null == okHttpClient){ synchronized (OkHttpUtils.class){ if (okHttpClient == null){ new OkHttpUtils(); return okHttpClient; } } } return okHttpClient; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
按上面调整后,效果好了很多。应用这个单例:
/** * * @param bytes 待上传文件 * @param ids 运单的batch_id * @param type 1 不正常 2 车辆登记 */ public static void uploadImage(byte[] bytes,String fileName,String ids,int type,long tonce,Callback callback){ if (bytes == null || bytes.length == 0 || TextUtils.isEmpty(fileName)) return; // URL String url = "http://192.168.10.114:8080?name="+ fileName +"&ids="+ids+"&type="+type; OkHttpClient client = getInstance(); if(client == null)return; MediaType mediaType = MediaType.parse("image/jpeg"); RequestBody body = RequestBody.create(mediaType,bytes); Request request = new Request.Builder() .url(url) .method("POST", body) .addHeader("user-agent", "android") .addHeader("x-userid", "87875415646")// 添加x-userid请求头 .addHeader("x-sessionkey", "151561155")// 添加x-sessionkey请求头 .addHeader("x-tonce", Long.valueOf(tonce).toString())// 添加x-tonce请求头 .addHeader("x-timestamp", Long.valueOf(tonce).toString())// 添加x-timestamp请求头 .addHeader("Content-Type", "image/jpeg") .build(); client.newCall(request).enqueue(callback); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/106908195
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)