package com.example.httplogginginterceptortest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = providerRetrofit();
IService iService = retrofit.create(IService.class);
HashMap<String, String> params = new HashMap<>();
params.put("userId", "3805");
params.put("token", "a8f801d8b556dfa1870c007d88709767");
params.put("groupId", String.valueOf(5));
params.put("type", String.valueOf(1));
params.put("page", String.valueOf(1));
params.put("pageSize", String.valueOf(5));
iService.getRankForGroupUser(params).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<PublicResponseEntity<List<RankForGroupUserEntity>>>() {
@Override
public void accept(PublicResponseEntity<List<RankForGroupUserEntity>> listPublicResponseEntity) throws Exception {
Log.i("zhang_xin","成功--------------------------------");
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.i("zhang_xin","失败--------------------------------");
}
});
}
private Retrofit providerRetrofit() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
try {
String text = URLDecoder.decode(message, "utf-8");
Log.e("zhang_xin", text);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("zhang_xin", message);
}
}
});
//这里可以builder(). 添加更多的内容 具体看需求
OkHttpClient mClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
// .connectTimeout(30, TimeUnit.SECONDS)
// .writeTimeout(10,TimeUnit.MINUTES)
// .readTimeout(10,TimeUnit.MINUTES)
.build();
//这行必须加 不然默认不打印
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://119.253.81.55/tkheal/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//添加HttpClient
.client(mClient).build();
return retrofit;
}
}
需要添加的依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
看下打印输出结果