RetrofitUtil

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());


                    }
                });



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值