package com.example.gjl.day07_xiawu.http;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Retrofit的工具类
* <p>
* 1.get
* 2.post
* 3.配置拦截器
* 4.上传图片
*/
public class RetrofitUtil {
private static RetrofitUtil retrofitUtil;
private final Retrofit retrofit;
private static final String TAG = "RetrofitUtil===";
//单利模式
public static RetrofitUtil getInstance() {
if (retrofitUtil == null) {
retrofitUtil = new RetrofitUtil();
}
Log.d(TAG, "---" + retrofitUtil);
return retrofitUtil;
}
//因为Retrofit需要初始化,所以在构造方法里面我们可以将Retrofit初始化
public RetrofitUtil() {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();
Retrofit.Builder builder = new Retrofit.Builder();
retrofit = builder.baseUrl(HttpConfig.base_url)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.client(client)//指定配置好okhttp客户端
.build();
Log.d(TAG, "初始化完毕---" + retrofit);
}
//通过反射创建子类
public <T> T createReq(Class<T> clz) {
T t = retrofit.create(clz);
Log.d(TAG, "创建服务子类------");
return t;
}
//拦截器
class MyIntercepter implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RequestBody body = request.body();
if (body instanceof FormBody) {
FormBody.Builder newBuilder = new FormBody.Builder();
for (int i = 0; i < ((FormBody) body).size(); i++) {
String key = ((FormBody) body).name(i);
String value = ((FormBody) body).value(i);
newBuilder.add(key, value);
}
newBuilder.add("source", "android");
FormBody body1 = newBuilder.build();
Request request1 = request.newBuilder().post(body1).build();
Response response = chain.proceed(request1);
return response;
}
return null;
}
}
}
//调用RetrofitUtil工具类
RetrofitUtils retrofitUtils = RetrofitUtils.getRetrofitUtils();
MyService myService = retrofitUtils.createRequest(MyService.class);
//调用方法
myService.getAd(map)
.subscribeOn(Schedulers.newThread())//将请求过程切换到一个线程
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ShouYeBean>() {
@Override
public void onCompleted() {
Log.d(TAG, "完成-----");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "e---"+e);
}
@Override
public void onNext(ShouYeBean shouYeBean) {
Log.d(TAG, "--"+shouYeBean.toString());
}
});
RetrofitUtil
最新推荐文章于 2024-03-29 09:59:10 发布