封装网络工具类(OkHttp_get请求)

首先写一个ICallback接口

package com.qh.***.***.net;

public interface ICallBack {

    void success(Object obj);

    void failed(Exception e);
}

其次,开始OKHttp get请求

package com.qh.***.***.net;

import android.os.Handler;

import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;



public class HttpUtils {
    private static final HttpUtils ourInstance = new HttpUtils();
    private final OkHttpClient client;
    private static volatile HttpUtils instance;
    private Handler handler = new Handler();
    public static HttpUtils getInstance() {
        if (instance == null){
            synchronized (HttpUtils.class){
                if (null == instance){
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }

    private HttpUtils() {
        client = new OkHttpClient();
    }
    public void get(String url, final ICallBack callBack, final Type type){
        Request request = new Request
                .Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.failed(e);
                    }
                });
            }

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


猜你喜欢

转载自blog.csdn.net/mingxiangzhimin/article/details/85112157
今日推荐