OKhttp 拦截器

 private String url;
    private OnokhttpLisennr onokhttpLisennr;


    public static OkUtil okUtil(){
        OkUtil okUtil = new OkUtil();
        return okUtil;
    }
   Handler hh=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
          int ii= msg.what;
          if (ii==0){
              onokhttpLisennr.succeed((String) msg.obj);
          }if (ii==1){
               onokhttpLisennr.error((String) msg.obj);
           }
       }
   };
    public void getOk(String url, final Map<String, String> mm){
        this.url=url;
        OkHttpClient builder = new OkHttpClient.Builder().addInterceptor(new Myin()).build();
        FormBody.Builder formBody=new FormBody.Builder();
        Set<String> strings = mm.keySet();
        for (String ss:strings) {
            String s = mm.get(ss);
            formBody.add(ss,s);
        }
        FormBody build = formBody.build();
        Request build1 = new Request.Builder().url(url).post(build).build();
        Call call = builder.newCall( build1);
        call.enqueue(new Callback() {


            @Override
            public void onFailure(Call call, IOException e) {
                Message msg=new Message();
                msg.obj="失败";
                msg.what=1;
                hh.sendMessage(msg);
            }


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


public  interface OnokhttpLisennr {
        void succeed(String ss);
        void error(String ee);


}
public void setokttp(OnokhttpLisennr onokhttpLisennr){
        this.onokhttpLisennr=onokhttpLisennr;
}












    class  Myin implements Interceptor {


        private FormBody.Builder builder;


        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            RequestBody body = request.body();
            if (body instanceof FormBody){
                builder = new FormBody.Builder();
                for(int i=0; i<((FormBody) body).size();i++){
                    String name = ((FormBody) body).name(i);
                    String value = ((FormBody) body).value(i);
                    builder.add(name,value);


                }






            }
            builder.add("source","android");
            FormBody build = builder.build();
            Request build1 = request.newBuilder().url(url).post(build).build();
            Response proceed = chain.proceed(build1);
            return proceed;




        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值