封装单例模式和拦截器的使用

本文介绍了如何创建一个封装的OkHttpUtils类,用于简化OkHttp的使用。类中包含了静态方法来执行GET和POST请求,并且利用HttpLoggingInterceptor实现日志拦截,以便于调试。同时,配置了缓存和超时设置。
摘要由CSDN通过智能技术生成
创建一个封装的类 OkHttpUtils
导入包  记得都是okhttp下的
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtils {
private static OkHttpClient okHttpClient;
    //私有化的构造器
    private OkHttpUtils(){}
    public static OkHttpClient getInstance()
    {
        if(okHttpClient==null)
        {
            synchronized (OkHttpUtils.class)
            {
                //缓存的目录
                File sd_cache=new File(Environment.getExternalStorageDirectory(),"ok_cache");
                //缓存存储的大小
                int cacheSize=1024*1024*10;
                //OkHttp3拦截器
                HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Log.i("拦截器xxx", message.toString());
                    }
                });
                //OkHttp3的拦截器日志分类  4种
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                okHttpClient=new OkHttpClient.Builder()
                        .addInterceptor(interceptor)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30,TimeUnit.SECONDS)
                        .connectTimeout(60,TimeUnit.SECONDS)
                        .cache(new Cache(sd_cache,cacheSize))
                        .build();
            }
        }
        return okHttpClient;
    }
    //get请求
    public static void doGet(String url, Callback callback)
    {
        OkHttpClient okHttpClient=getInstance();
        //创建Request
        Request request=new Request.Builder()
                .url(url)
                .build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);
    }
    //post请求
    public static void doPost(String url, Map<String, String> params, Callback callback)
    {
        OkHttpClient okHttpClient=getInstance();

        FormBody.Builder builder=new FormBody.Builder();
        if(params!=null)
        {
            for(String key:params.keySet())
            {
                builder.add(key,params.get(key));
            }
        }

        Request request=new Request.Builder()
                .post(builder.build())
                .url(url)
                .build();

        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);
    }
}
拦截器  依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值