我正在處理以下代碼以設置timeout
在httpClient
上。但是,我的代碼指出找不到setConnectTimeout
和setReadTimeout
。我究竟做錯了什麼?在改造中設置connectionTimeOut和SocketTimeout
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import in.futuretrucks.Constant.Constant;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import static java.util.concurrent.TimeUnit.*;
public class ServiceGeneratorFileUpload {
public static final String API_BASE_URL = Constant.BASE_URL;
private static OkHttpClient httpClient = new OkHttpClient();
httpClient.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
httpClient.setReadTimeout(15, TimeUnit.SECONDS);
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("cache-control", "no-cache")
.header("Content-Type", "multipart/form-data")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
//set logging interceptor. Disabled as of now. Useful to see request and response feature
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.interceptors().add(interceptor);
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
它在下面的鏈接[Duplicate]已經被Asnwerd(http://stackoverflow.com/questions/29380844/how-to-set-a-timeout-in-retrofit-library#answer-29380845) –