Okhttp的get、post封装

依赖

   implementation 'com.squareup.okhttp3:okhttp:3.7.0'
    implementation 'com.squareup.okio:okio:1.12.0'

/*Time:2019/4/11
 *Author:zhaozhiwei
 *Description:
 */public class OkHttp {
     private static Handler handler = new Handler();
     private static volatile OkHttp instance;
     private OkHttpClient client;

     private OkHttp() {
         // 初始化操作
         client = new OkHttpClient();
     }
     //单例模式
     public static OkHttp getInstance(){
         if (instance == null){
            synchronized (OkHttp.class){
               if (null == instance){
                   instance = new OkHttp();
               }
            }
         }
         return instance;
     }
     public void doGet(String url,final Class clazz,final NetCallback netCallback){
         //创建一个亲求对象
         Request request = new Request.Builder()
                 .get()
                 .url(url)
                 .build();
         //创建call对象
         Call call = client.newCall(request);
         call.enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {

             }

             @Override
             public void onResponse(Call call, Response response) throws IOException {
                 String string = response.body().string();
                 Gson gson = new Gson();
                 final Object o = gson.fromJson(string, clazz);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {
                         netCallback.onSuccess(o);
                     }
                 });
             }
         });
     }
     public static void doPost(String url, String name, String pwd, final Class clazz, final NetCallback netCallback){
         OkHttpClient okHttpClient = new OkHttpClient.Builder()
                 .connectTimeout(3000, TimeUnit.SECONDS)
                 .readTimeout(3000,TimeUnit.SECONDS)
                 .build();

         FormBody builder1 = new FormBody.Builder()
                 .add("phone",name)
                 .add("pwd",pwd)
                 .build();
         Request build = new Request.Builder()
                 .url(url)
                 .post(builder1)
                 .build();
         Call call = okHttpClient.newCall(build);
         call.enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {

             }

             @Override
             public void onResponse(Call call, Response response) throws IOException {
                 String string = response.body().string();
                 Gson gson = new Gson();
                 final Object o = gson.fromJson(string, clazz);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {
                         netCallback.onSuccess(o);
                     }
                 });
             }
         });

     }

    public interface NetCallback{
         void onSuccess(Object obj);
     }
}

**//可有可无的拦截器**
 //创建拦截器
    private Interceptor getAppInterceptor()
    {
        Interceptor interceptor= new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                Request request = chain.request();
                Log.e("++++++","拦截前");
                Response response = chain.proceed(request);
                Log.e("++++++","拦截后");
                return response;
            }
        };
        return interceptor;
    }
    //添加拦截器
     private OkHttp()
     {
         File file = new File(Environment.getExternalStorageDirectory(), "cache1");
         client = new OkHttpClient().newBuilder()
                 .readTimeout(3000,TimeUnit.SECONDS)
                 .connectTimeout(3000,TimeUnit.SECONDS)
                 .addInterceptor(getAppInterceptor())
                 .cache(new Cache(file,10*1024))
                 .build();
     }
!

猜你喜欢

转载自blog.csdn.net/Android_Mr_Zhao/article/details/89253215
今日推荐