| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.runt.open.mvi.OpenApplication; |
| | | import com.runt.open.mvi.data.PhoneDevice; |
| | | import com.runt.open.mvi.retrofit.net.NetWorkCost; |
| | | import com.runt.open.mvi.retrofit.net.NetWorkListenear; |
| | | import com.runt.open.mvi.retrofit.net.NetWorkListener; |
| | | import com.runt.open.mvi.retrofit.utils.HttpPrintUtils; |
| | | import com.runt.open.mvi.utils.DeviceUtil; |
| | | |
| | | import org.json.JSONArray; |
| | | import org.json.JSONException; |
| | |
| | | * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-10-21. |
| | | */ |
| | | |
| | | public class HttpLoggingInterceptor extends AddHeadersInterceptor { |
| | | public class HttpLoggingInterceptor extends OpenInterceptor { |
| | | |
| | | final String TAG = "HttpLogging"; |
| | | |
| | | private boolean printLog ; |
| | | |
| | | public HttpLoggingInterceptor(){ |
| | | this(true); |
| | | } |
| | | public HttpLoggingInterceptor(boolean printLog) { |
| | | this.printLog = printLog; |
| | | } |
| | | |
| | | @Override |
| | | public Response intercept(Chain chain) throws IOException { |
| | | |
| | | Request requestTemp = chain.request(); |
| | | int hashCode = requestTemp.hashCode(); |
| | | if(printLog) { |
| | | Log.d(TAG, "hashcode:" + hashCode); |
| | | } |
| | | Request.Builder requestBuild = requestTemp.newBuilder() |
| | | .addHeader("device", new Gson().toJson(PhoneDevice.getDevice())) |
| | | .addHeader("appVersion", DeviceUtil.getAppVersionName(OpenApplication.Companion.getApplication())) |
| | | .addHeader("os", DeviceUtil.isHarmonyOS()? "harmony" : "android"); |
| | | Log.d(TAG, "hashcode:" + hashCode); |
| | | Request.Builder requestBuild = addHeaders(requestTemp); |
| | | /*if(UserBean.getUser() != null){ |
| | | requestBuild.addHeader("token",UserBean.getUser().getToken()); |
| | | }*/ |
| | |
| | | try { |
| | | logArrays.addAll(getRequestLog(request)); |
| | | int position = logArrays.size() +2; |
| | | response = chain.proceed(request); |
| | | response = proceed(chain,request); |
| | | logArrays.addAll(getResponseLog(response)); |
| | | NetWorkCost netWorkCost = NetWorkListenear.workCostMap.get(hashCode); |
| | | NetWorkCost netWorkCost = NetWorkListener.workCostMap.get(hashCode); |
| | | if(netWorkCost != null) { |
| | | logArrays.add(position, "<-- costtimes : " + netWorkCost); |
| | | } |
| | | NetWorkListenear.workCostMap.remove(hashCode); |
| | | NetWorkListener.workCostMap.remove(hashCode); |
| | | new Thread(){ |
| | | @Override |
| | | public void run() { |
| | | if(printLog) { |
| | | HttpPrintUtils.getInstance().printLog(logArrays, true);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应 |
| | | } |
| | | HttpPrintUtils.getInstance().printLog(logArrays, true);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应 |
| | | } |
| | | }.start(); |
| | | } catch (JSONException e) { |
| | |
| | | e.printStackTrace(); |
| | | } catch (Exception e) { |
| | | logArrays.add("<-- response url:" + URLDecoder.decode(request.url().toString(), "UTF-8")); |
| | | NetWorkCost netWorkCost = NetWorkListenear.workCostMap.get(hashCode); |
| | | NetWorkCost netWorkCost = NetWorkListener.workCostMap.get(hashCode); |
| | | if (netWorkCost != null) { |
| | | netWorkCost.total = new Date().getTime() - netWorkCost.total; |
| | | logArrays.add("<-- costtimes : " + netWorkCost); |
| | | } |
| | | NetWorkListenear.workCostMap.remove(hashCode); |
| | | NetWorkListener.workCostMap.remove(hashCode); |
| | | logArrays.add("<-- response failed " + e.getLocalizedMessage()); |
| | | logArrays.add("<-- " + e.toString()); |
| | | new Thread(){ |
| | | @Override |
| | | public void run() { |
| | | if(printLog) { |
| | | HttpPrintUtils.getInstance().printLog(logArrays, false);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应 |
| | | } |
| | | HttpPrintUtils.getInstance().printLog(logArrays, false);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应 |
| | | } |
| | | }.start(); |
| | | throw e;//抛出异常,用于请求接收信息 |
| | |
| | | return response; |
| | | } |
| | | |
| | | protected Response proceed(Chain chain,Request request) throws IOException { |
| | | return chain.proceed(request); |
| | | } |
| | | |
| | | /** |
| | | * 请求数据信息 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | private ArrayList<String> getRequestLog(Request request) throws IOException, JSONException { |
| | | protected ArrayList<String> getRequestLog(Request request) throws IOException, JSONException { |
| | | RequestBody requestBody = request.body(); |
| | | ArrayList<String> logArrays = new ArrayList<>(); |
| | | String requestStartMessage = "--> " + request.method() + ' ' + URLDecoder.decode(request.url().toString() ,"UTF-8")+ ' ' ; |
| | |
| | | * @throws IOException |
| | | * @throws JSONException |
| | | */ |
| | | private ArrayList<String> getResponseLog(Response response) throws IOException, JSONException { |
| | | protected ArrayList<String> getResponseLog(Response response) throws IOException, JSONException { |
| | | ArrayList<String> logArrays = new ArrayList<>(); |
| | | ResponseBody responseBody = response.body(); |
| | | long contentLength = responseBody.contentLength(); |