okhttp封装

 public class Okhttputils {
private OkHttpClient client;
private static volatile Okhttputils instance;
private Handler handler = new Handler();

//创建拦截器
private Interceptor getInterpolator() {
    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Log.i("++++", "拦截前");
            Response response = chain.proceed(request);
            Log.i("++++", "拦截后");
            return response;
        }
    };
    return interceptor;
}

//添加拦截器
private Okhttputils() {
    File file = new File(Environment.getExternalStorageDirectory(), "cache1");
    client = new OkHttpClient().newBuilder()
            .readTimeout(3000, TimeUnit.SECONDS)
            .connectTimeout(3000, TimeUnit.SECONDS)
            .addInterceptor(getInterpolator())
            .cache(new Cache(file, 10 * 1024))
            .build();
}

//创建单例
public static Okhttputils getinstance() {
    if (instance == null) {
        synchronized (Okhttputils.class) {
            if (instance == null) {
                instance = new Okhttputils();
            }
        }
    }
    return instance;
}

//封装doget
public void doGet(String url, final Class cizz, final NetCallBack callback) {
    Request request = new Request.Builder()
            .get()
            .url(url)
            .build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String string = response.body().string();
            Gson gson = new Gson();
            final Object o = gson.fromJson(string, cizz);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callback.LoadSuccess(o);
                }
            });

        }
    });
}

//封装post
public void doPost(String url, final Class cizz, String name, String pwd, final NetCallBack callBack) {
    client = new OkHttpClient();
    FormBody formBody = new FormBody.Builder()
            .add("phone", name)
            .add("pwd", pwd)
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            callBack.LoadFail();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String string = response.body().string();
            Gson gson = new Gson();
            final Object o = gson.fromJson(string, cizz);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callBack.LoadSuccess(o);
                }
            });

        }
    });


}

public interface NetCallBack {
    void LoadSuccess(Object obj);

    void LoadFail();
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值