ok请求封装get , post 加 拦截器

package com.example.administrator.mp2.http;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/02/28.
 */

public class OkHttpUtils {
    private static MyHandler myHandler = new MyHandler();
    //单利模式
    public static OkHttpUtils okHttpUtils = null;
    private static OKloadListener okLoadListener;

    public static OkHttpUtils getInstance() {
        if (okHttpUtils == null) {
            okHttpUtils = new OkHttpUtils();
        }
        return okHttpUtils;
    }

    //ok  get请求
    public static void okGet(String url, String... params) {
        String newUrl = url + "?source=" + params[0] + "&pid=" + params[1];
        //请求对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //封装url地址
        Request request = new Request.Builder().url(newUrl).build();
        //请求队列
        Call call = okHttpClient.newCall(request);
        //y异步
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = myHandler.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                myHandler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = myHandler.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                myHandler.sendMessage(message);
            }
        });
    }

    //post请求
    public void okPost(String url, Map<String, String> params) {
        //请求对象        添加拦截器
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
        //body
        FormBody.Builder builder = new FormBody.Builder();
        //遍历集合
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            String value = params.get(key);
            builder.add(key, value);
        }
        FormBody body = builder.build();
        //封装url地址
        Request request = new Request.Builder().url(url).post(body).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = myHandler.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                myHandler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = myHandler.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                myHandler.sendMessage(message);
            }
        });
    }

    //拦截器
    class MyInterceptor 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 formBody = newBuilder.build();
                Request request1 = request.newBuilder().post(formBody).build();
                Response response = chain.proceed(request1);
                return response;
            }
            return null;
        }
    }

    //主线程  发送消息
    static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://成功
                    okLoadListener.okLoadSuccess((String) msg.obj);
                    break;
                case 1://失败
                    okLoadListener.okLoadError((String) msg.obj);
                    break;
            }
        }
    }

    //定义外部访问的方法    让接口对象  传进来
    public void setOnOkLoadListener(OKloadListener okLoadListener) {
        this.okLoadListener = okLoadListener;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值