OkHttpUtils的封装(实现登录与注册)

public class OkhttpUtils {


    private OkHttpClient okHttpClient;
    private static OkhttpUtils okhttpUtils;
    private final Handler handler;

    private OkhttpUtils() {
        //创建一个主线程的handler
        handler = new Handler(Looper.getMainLooper());
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .build();
    }

    //设置外部访问的方法
    public static OkhttpUtils getInstance() {
        if (okhttpUtils == null) {
            synchronized (OkhttpUtils.class) {
                if (okhttpUtils == null) {
                    return okhttpUtils = new OkhttpUtils();
                }
            }
        }
        return okhttpUtils;
    }


    //get请求
    public void doGet(String url, final onCallBack onCallBack) {

        final Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (onCallBack != null) {
//切换到主线程
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onCallBack.onFaild(e);
                        }
                    });
                }
            }
            @Override
            public void onResponse(Call call, final Response response) throws IOException {

                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        try{
                            if(response!=null&&response.isSuccessful()){
                                String json = response.body().string();
                                if(onCallBack!=null){
                                    onCallBack.onResponse(json);
                                    return;
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        if(onCallBack!=null){
                            onCallBack.onFaild(new Exception("异常"));
                        }
                    }
                });
            }
        });
    }
    //post请求
  public void doPost(String url, Map<String,String> map, final onCallBack onCallBack){

      FormBody.Builder builder = new FormBody.Builder();
      for(String key:map.keySet()){
          builder.add(key,map.get(key));
      }
      FormBody formBody = builder.build();
      Request request= new Request.Builder()
              .post(formBody)
              .url(url)
              .build();

      Call call = okHttpClient.newCall(request);
      call.enqueue(new Callback() {
          @Override
          public void onFailure(Call call, final IOException e) {

              if(onCallBack!=null){
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                       onCallBack.onFaild(e);
                      }
                  });
              }
          }

          @Override
          public void onResponse(Call call, final Response response) throws IOException {

              handler.post(new Runnable() {
                  @Override
                  public void run() {

                      try{
                          if(response!=null&&response.isSuccessful()){
                              String json = response.body().string();
                              if(onCallBack!=null){
                                  onCallBack.onResponse(json);
                                  return;
                              }
                          }
                      }catch (Exception e){
                          e.printStackTrace();
                      }
                      if(onCallBack!=null){
                         onCallBack.onFaild(new Exception("有异常"));
                      }
                  }
              });
          }
      });
  }
    //定义接口进行回调
    public interface onCallBack {
        void onFaild(Exception e);
        void onResponse(String json);
    }


}
展开阅读全文

没有更多推荐了,返回首页