Okttp的简单封装类log拦截器

import android.graphics.Bitmap;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

import static java.lang.String.valueOf;

/**
 * Created by asus on 2017/10/25.
 */

public class Okutils {
    public static final int TIMEOUT=1000*60;
    private static OkHttpClient client;
    public Okutils(){
     client=getInstance();
    }
    public static OkHttpClient getInstance() {
        if (client == null)
        {
            synchronized (Okutils.class)
            {
                if (client == null)
                {
                    client = new OkHttpClient.Builder()
                            .addInterceptor(new Loginterceptor())
                            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                            .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                            .retryOnConnectionFailure(false).build();
                }
            }
        }
        return client;
    }


public   void getdata(String url,final Backquer backquer){
    Request request=new Request.Builder().url(url).build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            backquer.onfailure(call,e);
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            backquer.onresponse(call,response);
        }
    });
}
    public  void  postdata(String url, Map<String,Object> map, final Backquer backquer){
    FormBody.Builder builder=new FormBody.Builder();
    //遍历map
    if(map!=null){
        for (Map.Entry<String,Object> entry : map.entrySet()){
            builder.add(entry.getKey(), entry.getValue().toString());
        }
    }
    RequestBody body = builder.build();
    Request request = new Request.Builder().url(url).post(body).build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            backquer.onfailure(call,e);
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            backquer.onresponse(call,response);
        }
    });
}

public   void updata(Bitmap photo){
    try {
    File file=new File("mnt/sdcard/icon.png");
    if(!file.exists()){
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    FileOutputStream  outputStream = new FileOutputStream(file);
    photo.compress(Bitmap.CompressFormat.PNG,100,outputStream);
    if(file!=null) {
        String filename = file.getName();
        Map<String, Object> params = new HashMap<>();
        params.put("uid", 1);
        OkHttpClient okHttpClient = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        builder.addFormDataPart("file", filename, requestBody);
        if (params != null) {
            // map 里面是请求中所需要的 key 和 value
            for (Map.Entry entry : params.entrySet()) {
                builder.addFormDataPart(valueOf(entry.getKey()), valueOf(entry.getValue()));
            }
        }
        Request request = new Request.Builder().url("http://120.27.23.105/file/upload").post(builder.build()).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

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

            }
        });
    }
} catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
    static class  Loginterceptor implements Interceptor {

        public String TAG = "LogInterceptor";

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            long startTime = System.currentTimeMillis();
            Response response = chain.proceed(chain.request());
            long endTime = System.currentTimeMillis();
            long duration=endTime-startTime;
            MediaType mediaType = response.body().contentType();
            String content = response.body().string();
            Log.d(TAG,"\n");
            Log.d(TAG,"----------Start----------------");
            Log.d(TAG, "| "+request.toString());
            String method=request.method();
            if("POST".equals(method)){
                StringBuilder sb = new StringBuilder();
                if (request.body() instanceof FormBody) {
                    FormBody body = (FormBody) request.body();
                    for (int i = 0; i < body.size(); i++) {
                        sb.append(body.encodedName(i) + "=" + body.encodedValue(i) + ",");
                    }
                    sb.delete(sb.length() - 1, sb.length());
                    Log.d(TAG, "| RequestParams:{"+sb.toString()+"}");
                }
            }
            Log.d(TAG, "| Response:" + content);
            Log.d(TAG,"----------End:"+duration+"毫秒----------");
            return response.newBuilder()
                    .body(ResponseBody.create(mediaType, content))
                    .build();
        }
    }
    public interface  Backquer{
        void  onfailure(Call call, IOException e);
        void  onresponse(Call call, Response response);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值