Okhttp的Get_Poast请求封装

来源:互联网 发布:sql培训学校 编辑:程序博客网 时间:2024/06/06 15:39

//post   Ok

package com.example.lxgwc.myokhttp;import android.os.Handler;import android.util.Log;import com.example.lxgwc.twoxaingqing.Okinteface;import java.io.IOException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class EdOkthhp {    private Handler handler=new Handler();    private static EdOkthhp okHttp;    private String string;    private String message;    private String result;    private EdOkthhp(){    }    public static EdOkthhp getInert(){        if(null == okHttp){            synchronized (EdOkthhp.class){              okHttp=new EdOkthhp();            }        }        return okHttp;    }   public  void doGet(String path, Map<String,String> map, final Okinteface okinteface){       StringBuffer sb=null;          for(String key:map.keySet()){              if(sb==null){                  sb=new StringBuffer();                  sb.append("?");              }else{                  sb.append("&");              }              sb.append(key).append("=").append(map.get(key));          }       OkHttpClient okHttpClient = new OkHttpClient();       final Request request = new Request.Builder()                    .get()                    .url(path+sb.toString())                    .build();       Log.e("arr",path+sb.toString());       Call call = okHttpClient.newCall(request);       call.enqueue(new Callback() {           @Override           public void onFailure(Call call, final IOException e) {               handler.post(new Runnable() {                   @Override                   public void run() {                       okinteface.shibai(e.getMessage());                   }               });           }           @Override           public void onResponse(Call call, final Response response) {               try {                   string = response.body().string();               } catch (IOException e) {                   e.printStackTrace();               }               handler.post(new Runnable() {                   @Override                   public void run() {                       okinteface.chenggong(string);                   }               });           }       });   }        public void post(String url, Map<String,String> map, final Okinteface okinteface){        //1:创建OkHttpClient对象        OkHttpClient okHttpClient = new OkHttpClient();        //2:提供post请求需要的body对象        FormBody.Builder builder = new FormBody.Builder();        for(Map.Entry<String,String> entry:map.entrySet()){            builder.add(entry.getKey(),entry.getValue());        }        FormBody body = builder.build();        //3:创建Request对象        final Request request = new Request.Builder()                .post(body)                .url(url)                .build();        //4:创建Call对象        Call call = okHttpClient.newCall(request);        //5:请求网络        call.enqueue(new Callback() {            //请求失败            @Override            public void onFailure(Call call, final IOException e) {                message = e.getMessage();                handler.post(new Runnable() {                    @Override                    public void run() {//                        callBack.onFailed(e);                        okinteface.shibai(message);                    }                });            }            //请求成功            @Override            public void onResponse(Call call, Response response) throws IOException {                result = response.body().string();                //拿到数据解析//                final Object o = new Gson().fromJson(result, c);                //当前是在子线程,回到主线程中                handler.post(new Runnable() {                    @Override                    public void run() {                        //回调//                        callBack.onSuccess(o);                        okinteface.chenggong(result);                    }                });            }        });    }}


原创粉丝点击