展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

okhttp基础使用(二)

  • 新建类自定义拦截器
package com.example.okhttp;

import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LogIntercept implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request=chain.request();
        long curTime =System.currentTimeMillis();
        Log.i("TAG","intercept: REQUEST="+ request.toString());
        Response response=chain.proceed(request);
        Log.i("TAG","intercept: REQUEST="+ response.toString());
        Log.i("TAG","intercept:耗时="+(System.currentTimeMillis() - curTime) + "ms");
        return response;
    }
}
  • 添加到MainActivity
        okHttpclient = new OkHttpClient.Builder()
                .addInterceptor(new LogIntercept())
                .build();
posted @ 2024-08-05 18:41  DogLeftover  阅读(3)  评论(0编辑  收藏  举报