package com.example.administrator.mvplibao.utils.my_butter_knife.okhttp; import android.util.Log; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * Created by Administrator on 2016/9/26. * 它里面有两个方法get和post */ public class OkHttpUtils { OkHttpClient client = new OkHttpClient(); private static OkHttpUtils instance; ResultResolve resultHandler; FailResolve failResolve; //可以采用注入的实现(传入你处理实现的接口处理) public void setOnHandler(ResultResolve resultHandler){ this.resultHandler = resultHandler; } //这里传入的是解析失败后的实现接口 public void setOnFail(FailResolve failResolve){ this.failResolve = failResolve; } private OkHttpUtils(){ } /** * 这里为get请求 * @param path */ public void getDate(String path){ if(resultHandler==null){ throw new RuntimeException("你必须要传入处理方法"); } Request.Builder builder = new Request.Builder(); Request request = builder.url(path).get().build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { if(failResolve!=null) failResolve.failResolve(); } @Override public void onResponse(Call call, Response response) throws IOException { resultHandler.resolve(response); } }); } /** * 这里为post请求 * @param path * @param heads 请求头封装为map集合里 * @param bodys 请求体封装为map集合里 */ public void postDate(String path, Map<String,String> heads,Map<String,String> bodys){ if(resultHandler==null){ throw new RuntimeException("你必须要传入处理方法"); } //创建builder对象 Request.Builder builder = new Request.Builder(); //把地址添加进去 builder.url(path); //添加post所需要的头 if(heads!=null){ for(Map.Entry<String,String> data:heads.entrySet()){ builder.addHeader(data.getKey(),data.getValue()); } } //添加post需要的内容体 if(bodys!=null){ FormBody.Builder fb = new FormBody.Builder(); for(Map.Entry<String,String> data:bodys.entrySet()){ try { fb.add(data.getKey(), URLEncoder.encode(data.getValue(),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } builder.post(fb.build()); } Request request = builder.build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { if(failResolve!=null) failResolve.failResolve(); } @Override public void onResponse(Call call, Response response) throws IOException { resultHandler.resolve(response); } }); } //这里采用单列模式 public static OkHttpUtils getInstance(){ if(instance!=null) return instance; instance = new OkHttpUtils(); return instance; } }package com.example.administrator.mvplibao.utils.my_butter_knife.okhttp; /** * Created by Administrator on 2016/9/26. */ public interface FailResolve { public void failResolve(); }package com.example.administrator.mvplibao.utils.my_butter_knife.okhttp; import okhttp3.Response; /** * Created by Administrator on 2016/9/26. */ public interface ResultResolve { public void resolve(Response response); }
写个OKHttpUtils
最新推荐文章于 2024-04-02 10:37:15 发布