Java 无法停止改装发送

Java 无法停止改装发送,java,android,sockets,retrofit2,okhttp3,Java,Android,Sockets,Retrofit2,Okhttp3,我使用改装和OkHttp3库向服务器发送一些消息,并将其设置为: okClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15,TimeUnit.SECONDS) .addInt

我使用改装和OkHttp3库向服务器发送一些消息,并将其设置为:

okClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15,TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();
当我想发送一条大消息(例如,大约需要2分钟)时,Reformation会完全发送我的文件,2分钟后,我会收到
TimeOut
消息。如果我希望在15秒后停止发送并显示错误消息

是否有我必须遵守的特定项目?请引导我

或者建议我一个标准的方法在15秒后打破这个操作

霉菌代码:

class RetrofitFactory {
private static final RetrofitFactory INSTANCE = new RetrofitFactory();
public static RetrofitFactory getInstance() {
    return INSTANCE;
}

public OkHttpClient getOkHttp()
{
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    okClient = new OkHttpClient.Builder()
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(15,TimeUnit.SECONDS)
            .addInterceptor(new GzipRequestInterceptor())
            .addInterceptor(interceptor)
            .build();
    return okClient;
}

public myInterface getlimit()
{
    if (retrofit == null) {
            OkHttpClient okClient = getOkHttp();
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            retrofit = new Retrofit.Builder()
                    .client(okClient)
                    .baseUrl(BuildConfig.BASEURL)
                    .addConverterFactory(JacksonConverterFactory.create(objectMapper))
                    .build();
    }

    return retrofit.create(myInterface.class);
}
}
public interface myInterface{
    @POST("api/ReadingApi/Something")
    Call<Something> DoReading(
            @Body List<Something> list,
            @Header("Authorization") String auth);

}


Call<DoReadResult> x = RetrofitFactory.getInstance().getlimit().DoReading(
                            data, "Something");

response = x.execute();

正如您所说,您正在使用改装,因此您需要通过改装通话轻松取消通话:

Call<ResponseBody> call =  
    uploadService.uploadSomething(fileUrl);
call.enqueue(new Callback<ResponseBody>() {  
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Log.d(TAG, "request success");
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e(TAG, "request failed");
    }
});
    }

call.cancel();  
Call=
uploadService.uploadSomething(文件URL);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(标记“请求成功”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(标记“请求失败”);
}
});
}
call.cancel();
使用call.cancel();你可以取消你的请求

请参阅此处的更多信息:


正如您所说,您正在使用改装,因此您需要使用改装电话轻松取消通话:

Call<ResponseBody> call =  
    uploadService.uploadSomething(fileUrl);
call.enqueue(new Callback<ResponseBody>() {  
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Log.d(TAG, "request success");
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e(TAG, "request failed");
    }
});
    }

call.cancel();  
Call=
uploadService.uploadSomething(文件URL);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(标记“请求成功”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(标记“请求失败”);
}
});
}
call.cancel();
使用call.cancel();你可以取消你的请求

请参阅此处的更多信息:



这意味着您希望在15秒后取消请求,但如果需要更多时间,则不会有结果?是的,我想取消我的请求您的OkHttpClient库版本是什么?com.squareup.okhttp3:logging interceptor:3.10.0这意味着您希望在15秒后取消请求,但如果需要更多时间,则不会有结果?是的,我想取消我的请求您的OkHttpClient库版本是什么?com.squareup.okhttp3:日志侦听器:3.10.0所以当我们使用
超时时
?你能告诉我更多吗?你确定你通过了okhttp进行了改装吗?如何通过okhttp配置超时设置:是的,我做了所有这些。把代码的那一部分放进去,我认为你做错了什么,在看不到代码的时候帮不上忙。所以当我们使用
Timeout
?你能告诉我更多吗?你确定你通过了okhttp进行了改造吗?如何通过okhttp配置超时设置:是的,我做了所有这些。把代码的那部分放进去,我认为你做错了什么,在看不到代码的时候帮不上忙。