OkHttp的封装

来源:互联网 发布:淘宝网牛仔女裤 编辑:程序博客网 时间:2024/06/18 08:37
import android.os.Handler;import android.os.Message;import android.util.Log;import com.google.gson.Gson;import java.io.IOException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;/** *  */public class OkHttp {    private  static Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);        }    };    //单例模式    private static volatile OkHttp instance;    public static OkHttp getinstance() {        if (instance == null) {            synchronized (OkHttp.class) {                if (instance == null) {                    instance = new OkHttp();                }            }        }        return instance;    }    //post请求    public void postData(String url, Map<String, String> map, final AllBack allback, final Class clazz, final String tag) {        //对参数做拼接处理        StringBuffer buffer = new StringBuffer();        buffer.append(url);        //如果存在?        if (buffer.indexOf("?") != -1) {            //如果?不在最后一位            if (buffer.indexOf("?") != buffer.length() - 1) {                buffer.append("&");            }        }else{            buffer.append("?");        }        for (Map.Entry<String, String> entry : map.entrySet()) {            buffer.append(entry.getKey())                    .append("=")                    .append(entry.getValue())                    .append("&");        }        buffer.deleteCharAt(buffer.lastIndexOf("&"));        OkHttpClient client = new OkHttpClient.Builder()                .build();        Request request = new Request.Builder()                .get()                .url(buffer.toString())                .build();        Call call = client.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                allback.onFailed(tag,e.getMessage());            }            @Override            public void onResponse(Call call, Response response) throws IOException {                String string = response.body().string();                Log.i("ttt",string.toString());                Gson gson = new Gson();                final Object o = gson.fromJson(string, clazz);                handler.post(new Runnable() {                    @Override                    public void run() {                        allback.onSuccess(tag,o);                    }                });            }        });    }    //get请求    public  void getData(String url, final AllBack allback,final Class clazz,final String tag){        OkHttpClient client = new OkHttpClient.Builder().build();        Request request = new Request.Builder()                .get()                .url(url)                .build();        Call call = client.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                allback.onFailed(tag,e.getMessage());            }            @Override            public void onResponse(Call call, Response response) throws IOException {                final String string = response.body().string();                Log.i("iii",string);                Gson gson = new Gson();                final Object o = gson.fromJson(string, clazz);                handler.post(new Runnable() {                    @Override                    public void run() {                        allback.onSuccess(tag,o);                    }                });            }        });    }}
public interface AllBack {    void onSuccess(String tag, Object o);    void onFailed(String tag, String e);}

public interface INewsView {    void success(List<Imguser> list);    void failed(String tag, String e);}

 

原创粉丝点击