okHttputils二次封装

OkHttpUtols

public class OkHttpUtils {

    private static OkHttpUtils okHttpUtils;
    private OkHttpClient okHttpClient;
 
    private OkHttpUtils(){
//创建拦截器对象
HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor();
//配置日志拦截器
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient= new OkHttpClient.Builder()
                .writeTimeout(2000,TimeUnit.MICROSECONDS)
                .
.addInterceptor(httpLoggingInterceptor)
                .build();

    }

    public static OkHttpUtils getinstance(){

        if(okHttpUtils==null){
           synchronized (OkHttpUtils.class){
               if(okHttpUtils==null){
                   okHttpUtils=new OkHttpUtils();
               }

           }
        }

        return okHttpUtils;
    }


    //get封装
    public void getData(String url,HashMap<String,String>parms,final RequestCallBack requestCallBack ){

        StringBuilder urlsb = new StringBuilder();
        String allUrl = "";

        for (Map.Entry<String,String> stringStringEntry:parms.entrySet() ){

            urlsb.append("?").append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("&");


        }
        allUrl = url+urlsb.toString().substring(0,urlsb.length()-1);
        System.out.println("url:" + allUrl);


        Request request =new Request.Builder()
                .url(allUrl)
                .get()
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {
                if(requestCallBack!=null){
                    requestCallBack.failure(call,e);
                }
            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(requestCallBack!=null){
                    requestCallBack.onResponse(call,response);
                }
            }
        });


    }


    //post请求方式
    public void postData(String url,HashMap<String,String>parms,final RequestCallBack requestCallBack){

        FormBody.Builder formBodybuilder = new FormBody.Builder();

        if(parms!=null && parms.size()>0){
            for (Map.Entry<String,String> stringStringEntry:parms.entrySet() ){
                formBodybuilder.add(stringStringEntry.getKey(),stringStringEntry.getValue());
            }
        }
        Request request = new Request.Builder()
                .url(url)
                .post(formBodybuilder.build())
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (requestCallBack != null) {
                    requestCallBack.failure(call, e);
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (requestCallBack != null) {
                     requestCallBack.onResponse(call,response);
                }
            }
        });



    }





}

 

RequestCallBack

interface RequestCallBack {
    void failure(Call call, IOException e);
    void onResponse(Call call, Response response);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值