ok

import java.io.IOException;
import java.text.Format;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by hasee on 2018/6/15.
 */

public class Okhttutlis {

  private static  Okhttutlis okhttutlis;

  private Handler mhandler;
    private final OkHttpClient mokHttpClient;


    private  Okhttutlis(){

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Log.d("TAG",message);
                    }
                }
        );

        //转为主线程
      mhandler=  new Handler(Looper.getMainLooper());

        mokHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .readTimeout(5000,TimeUnit.MILLISECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .build();

            }
//单例模式  没有就new创建
    public  static  Okhttutlis getInstance(){

          if (okhttutlis==null){

            return  okhttutlis= new Okhttutlis();

          }
      return  okhttutlis;
    }


    public  void doPost(String url, Map<String,String> map, final OkCallback okCallback){

        FormBody.Builder builder = new FormBody.Builder();
         for (String key:map.keySet()){
             builder.add(key,map.get(key));
         }
        FormBody formBody = builder.build();
        final Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();
        Call call = mokHttpClient.newCall(request);
        call.enqueue(new Callback() {
            
            private String string;
            @Override
            public void onFailure(Call call, final IOException e) {
                if (okCallback!=null){
                    mhandler.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallback.onFailure(e);
                        }
                    });


                }

            }

            @Override
            public void onResponse(Call call,final Response response) throws IOException {
            if (response!=null&&response.isSuccessful()){

                                  string = response.body().string();
            }
                    mhandler.post(new Runnable() {
                        @Override
                        public void run() {
                          try {

                  

                                  if (okCallback!=null){

                                      okCallback.onResponse(string);
                                      Log.d("TAG","onResponse"+string);
                                      return;
                                  }
                              
                          }catch (Exception e){
                              e.printStackTrace();
                          }
                            if(okCallback!=null){

                                okCallback.onFailure(new Exception("网络异常"));

                            }
                        }

                    });


            }
        });



    }

    public  void doGet(String url, final OkCallback okCallback){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = mokHttpClient.newCall(request);

        call.enqueue(new Callback() {
            private String string;
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG","okhttp  onFailure失败"+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                   if (response!=null&&response.isSuccessful()){
                       string = response.body().string();

                   }
                if (okCallback!=null){
                    mhandler.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallback.onResponse(string);
                        }
                    });

                }
            }
        });

    }
    public interface OkCallback {
        void onFailure(Exception e);

        void onResponse(String json);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想不上班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值