2016-02-29 54 views
0

我正在处理以下代码以设置timeouthttpClient上。但是,我的代码指出找不到setConnectTimeoutsetReadTimeout。我究竟做错了什么?在改造中设置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); 
    } 


} 
+0

它在下面的链接[Duplicate]已经被Asnwerd(http://stackoverflow.com/questions/29380844/how-to-set-a-timeout-in-retrofit-library#answer-29380845) –

回答

0

添加库相关

在的build.gradle,加入这一行:

编译 'com.squareup.okhttp:okhttp:XXX'

也许这个链接可以帮助你: How to set timeout in Retrofit library?

+0

I已经包括它,但它仍然显示相同 –

0

自己找到了解决方案。 ServiceGeneratorFileUpload

public class ServiceGeneratorFileUpload { 

public static final String API_BASE_URL = Constant.BASE_URL; 

private static OkHttpClient getClient() { 
    OkHttpClient client = new OkHttpClient(); 
    client.setConnectTimeout(4, TimeUnit.MINUTES); 
    client.setReadTimeout(4, TimeUnit.MINUTES); 
    return client; 
} 

private final static OkHttpClient httpClient = getClient(); 

private static Retrofit.Builder builder = 
     new Retrofit.Builder() 
       .baseUrl(API_BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()); 

public static <S> S createService(Class<S> serviceClass) { 
    Retrofit retrofit = builder.client(httpClient).build(); 
    return retrofit.create(serviceClass); 
    } 
} 

现在有了上面的代码,我可以用来创建我的API服务......在改造

MyApiService myapiservice = ServiceGeneratorFileUpload.createService(MyApiService.class) 

使用文档来了解MyApiService是如何创建的。

相关问题