gpt4 book ai didi

android - 使用 OkhttpClient 3.2 和 Retrofit 2 发送视频时出现 java.net.SocketTimeoutException

转载 作者:太空狗 更新时间:2023-10-29 13:06:33 28 4
gpt4 key购买 nike

我使用 OkhttpClient 向使用 OkhttpClient 3.2Retrofit2 的服务器发送一个 15 秒长的视频,有时它工作正常但有时我得到以下错误,which很不一致。

java.net.SocketTimeoutException: timeout

这是我将视频发送到服务器的代码

private void sendVideoToServer( final String videoFilePath,final String api_key)
{
File videoFile = new File(videoFilePath);
RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), videoFile);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);

OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request.Builder ongoing = chain.request().newBuilder();
ongoing.addHeader("authorization", api_key);
return chain.proceed(ongoing.build());
}
})
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();

VideoInterface videoInterface = retrofit.create(VideoInterface.class);
Call<ResultObject> serverCom = videoInterface.sendVideoToServer(vFile);

serverCom.enqueue(new Callback<ResultObject>() {
@Override
public void onResponse(Call<ResultObject> call, retrofit2.Response<ResultObject> response) {
ResultObject result = response.body();
if(!TextUtils.isEmpty(result.getSuccess())){
Log.d("video Result " , result.getSuccess());
}
}

@Override
public void onFailure(Call<ResultObject> call, Throwable t) {
Log.d("video error",t.toString());
}
});
}

看了一些问题后,我尝试如下设置OkhttpClient的连接超时,但仍然无法解决问题。

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request.Builder ongoing = chain.request().newBuilder();
ongoing.addHeader("authorization", api_key);
return chain.proceed(ongoing.build());
}
});
builder.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES);
OkHttpClient httpClient = builder.build();

我完全不知道这部分做错了什么,请有人给我一些指导。

最佳答案

尝试传递实际的文件对象

  file = new File(tasks.getImageUrl());
requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);



sendVideo(body);

关于android - 使用 OkhttpClient 3.2 和 Retrofit 2 发送视频时出现 java.net.SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155883/

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