2016-02-29 87 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是如何創建的。

相關問題