OkHttp的封装及使用方法

    这里主要是使用OkHttp封装  直接将接口和参数还有泛型以及请求类型直接传入就可以进行解析获取到类的实例!!

package com.bwie.qieziyisheng.utils;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

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

public class OkHttpUtils {
    //声明
    private static volatile OkHttpUtils okHttpUtils;
    Handler handler;
    private final Gson gson;
    //声明接口
    MCallBack mCallBack;

    // 私有构造方法
    private OkHttpUtils() {
        handler = new Handler(Looper.getMainLooper());
        gson = new Gson();
    }

    //设置方法
    public static OkHttpUtils getOkHttpUtils() {
        if (null == okHttpUtils) {
            synchronized (OkHttpUtils.class) {
                if (null == okHttpUtils) {
                    okHttpUtils = new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }

    //请求网络
    public <T> void getMotheds(String url, final Class<T> cls) {
        //得到Client对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request
        Request request = new Request.Builder().url(url).build();
        //new Call
        Call call = okHttpClient.newCall(request);
        //接口回调
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                if (mCallBack != null) {
                    mCallBack.onFailure(request, e);
                }
            }

            @Override
            public void onResponse(Response response) throws IOException {
                //设置解析
                String json = response.body().string();
                getJson(json, cls);
            }
        });
    }

    //设置拼接字符串的方法
    public <T> void get(String url, Map<String, String> map, Class<T> cls) {
        int i = 0;
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            //获取Value
            String value = map.get(key);
            //拼接字符串
            if (i == 0) {
                url += key + "=" + value;
                i++;
            } else {
                url += "&" + key + "=" + value;
            }
        }
        //解析
        getMotheds(url, cls);
    }

    //解析
    public <T> void getJson(String json, Class<T> cls) {

        T t = gson.fromJson(json, cls);
        mainThread(t);
    }
    //设置请求Post请求
    public <T> void post(String url, Map<String,String> map, final Class<T> cls){
        //得到Client对象
        OkHttpClient okHttpClient=new OkHttpClient();
        //得到Body
        FormEncodingBuilder builder=new FormEncodingBuilder();
        //遍历
        Iterator<String> iterator=map.keySet().iterator();
        while(iterator.hasNext()){
            String key=iterator.next();
            //获取Value
            String value=map.get(key);
            //加入到Builder里面
            builder.add(key,value);
        }
        //得到Result
        Request request=new Request.Builder().url(url).post(builder.build()).build();

        //获取call
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                if(mCallBack!=null){
                    mCallBack.onFailure(request,e);
                }
            }

            @Override
            public void onResponse(Response response) throws IOException {
                //获取Json
                String json=response.body().string();
                getJson(json,cls);
            }
        });

    }

    //将消息发送到主线程
    public void mainThread(final Object result) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                //接口回调
                mCallBack.onResponse(result);
            }
        });
    }

    //设置接口
    public interface MCallBack {
        void onResponse(Object object);

        void onFailure(Request request, IOException e);
    }

    //设置接口
    public void setmCallBack(MCallBack callBack) {
        this.mCallBack = callBack;
    }
    //请求网络
    public <T> void initNetData(String url,Map<String,String> map,Class<T> cls,Methods methods){
        switch (methods){
            case GET:
               get(url,map,cls);
                break;
            case  POST:
                post(url,map,cls);
                break;
        }
    }


    //生成枚举
    public enum Methods {
        GET,POST

    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值