如何使用Retrofit OkHttpClient保持会话

时间:2016-04-19 01:11:14

标签: android retrofit

使用DefaultHttpClient()可以

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url_login.toURI());
HttpGet request1 = new HttpGet(url_getList.toURI());
HttpGet request2 = new HttpGet(url.getOtherList.toURI());
HttpResponse response = client.execute(request);
HttpResponse response1 = client.execute(request1);
HttpResponse response2 = client.execute(request2);

并且客户端会保留会话,如何通过改造2来管理以下内容

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();

                    // Customize the request
                    Request request = original.newBuilder()
                            .header("Accept", "application/json")
                            .header("Authorization", "auth-token")
                            .method(original.method(), original.body())
                            .build();

                    Response response = chain.proceed(request);

                    // Customize or return the response
                    return response;
                }
            }).build();

            Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create());

            Retrofit rt = builder.client(client).build();
            RestAPI restAPI = rt.create(RestAPI.class);
            ....
            callLogin = restAPI.login();
            Call<Model> callModel = restAPI.getModel();

2 restApi调用不起作用restApi客户端未登录

2 个答案:

答案 0 :(得分:9)

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            // init cookie manager
            CookieHandler cookieHandler = new CookieManager();

            client = new OkHttpClient.Builder().addNetworkInterceptor(interceptor)
                    .cookieJar(new JavaNetCookieJar(cookieHandler))
                    .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .build();

            Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create());

答案 1 :(得分:1)

在科特林: 将实现“ com.squareup.okhttp3:okhttp-urlconnection:3.8.1”添加到build.gradle中。

var cookieHandler: CookieHandler = CookieManager()

private var retrofit: Retrofit? = null
retrofit = Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(client)
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .build()

private val client : OkHttpClient
            private get() {
                val builder = OkHttpClient.Builder()
                builder.cookieJar(JavaNetCookieJar(cookieHandler))
                builder.connectTimeout(15, TimeUnit.SECONDS)
                builder.readTimeout(20, TimeUnit.SECONDS)
                builder.writeTimeout(20, TimeUnit.SECONDS)
                builder.retryOnConnectionFailure(true)
                return builder.build()
            }