Commit 0078b4c5 by 刘鹏

feat: api埋点完成,工具类迁移到net库

parent 8b9a99be
...@@ -44,7 +44,7 @@ ext { ...@@ -44,7 +44,7 @@ ext {
//以下 几乎不会动 //以下 几乎不会动
"router" : "0.0.1", "router" : "0.0.1",
"ydl-net" : "0.0.3.3", "ydl-net" : "0.0.3.6",
"ydl-utils" : "0.0.3.1", "ydl-utils" : "0.0.3.1",
] ]
ydl_app = [ ydl_app = [
...@@ -126,7 +126,7 @@ ext { ...@@ -126,7 +126,7 @@ ext {
//以下 几乎不会动 //以下 几乎不会动
"router" : "0.0.1", "router" : "0.0.1",
"ydl-net" : "0.0.3.3", "ydl-net" : "0.0.3.6",
"ydl-utils" : "0.0.3.1", "ydl-utils" : "0.0.3.1",
] ]
...@@ -199,6 +199,7 @@ ext { ...@@ -199,6 +199,7 @@ ext {
"androideventbus" : "org.simple:androideventbus:1.0.5.1", "androideventbus" : "org.simple:androideventbus:1.0.5.1",
"otto" : "com.squareup:otto:1.3.8", "otto" : "com.squareup:otto:1.3.8",
"gson" : "com.google.code.gson:gson:2.8.5", "gson" : "com.google.code.gson:gson:2.8.5",
"aliyun" : "com.aliyun.openservices:aliyun-log-android-sdk:2.5.13",
"javax.annotation" : "javax.annotation:jsr250-api:1.0", "javax.annotation" : "javax.annotation:jsr250-api:1.0",
"arouter" : "com.alibaba:arouter-api:1.4.1", "arouter" : "com.alibaba:arouter-api:1.4.1",
"progressmanager" : "me.jessyan:progressmanager:1.5.0", "progressmanager" : "me.jessyan:progressmanager:1.5.0",
......
...@@ -41,6 +41,7 @@ dependencies { ...@@ -41,6 +41,7 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api rootProject.ext.dependencies["gson"] api rootProject.ext.dependencies["gson"]
api rootProject.ext.dependencies["aliyun"]
api rootProject.ext.dependencies["rxjava2"] api rootProject.ext.dependencies["rxjava2"]
api(rootProject.ext.dependencies["rxandroid2"]) { api(rootProject.ext.dependencies["rxandroid2"]) {
exclude module: 'rxjava' exclude module: 'rxjava'
......
...@@ -7,6 +7,8 @@ import com.ydl.ydlnet.utils.CharacterHandler; ...@@ -7,6 +7,8 @@ import com.ydl.ydlnet.utils.CharacterHandler;
import com.ydl.ydlnet.utils.NetLogUtils; import com.ydl.ydlnet.utils.NetLogUtils;
import com.ydl.ydlnet.utils.UrlEncoderUtils; import com.ydl.ydlnet.utils.UrlEncoderUtils;
import com.ydl.ydlnet.utils.ZipHelper; import com.ydl.ydlnet.utils.ZipHelper;
import com.ydl.ydlnet.utils.log.AliYunLogConfig;
import com.ydl.ydlnet.utils.log.AliYunLogHelper;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
...@@ -23,13 +25,14 @@ import okhttp3.Response; ...@@ -23,13 +25,14 @@ import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import okio.Buffer; import okio.Buffer;
import okio.BufferedSource; import okio.BufferedSource;
/** /**
* Created by haorui on 2019-09-01 . * Created by haorui on 2019-09-01 .
* Des: 解析框架中的网络请求和响应结果并打印 * Des: 解析框架中的网络请求和响应结果并打印
*/ */
public class RequestLogInterceptor implements Interceptor { public class RequestLogInterceptor implements Interceptor {
private DefaultFormatPrinter mPrinter = new DefaultFormatPrinter(); private DefaultFormatPrinter mPrinter = new DefaultFormatPrinter();
private boolean mIsDebug ; private boolean mIsDebug;
private RequestHandler mRequestHandler; private RequestHandler mRequestHandler;
public RequestLogInterceptor(boolean isDebug, RequestHandler requestHandler) { public RequestLogInterceptor(boolean isDebug, RequestHandler requestHandler) {
...@@ -48,10 +51,10 @@ public class RequestLogInterceptor implements Interceptor { ...@@ -48,10 +51,10 @@ public class RequestLogInterceptor implements Interceptor {
mPrinter.printFileRequest(request); mPrinter.printFileRequest(request);
} }
if (mRequestHandler != null) if (mRequestHandler != null) {
request = mRequestHandler.onHttpRequestBefore(chain, request); request = mRequestHandler.onHttpRequestBefore(chain, request);
}
long t1 =System.nanoTime() ; long t1 = System.nanoTime();
Response originalResponse; Response originalResponse;
try { try {
originalResponse = chain.proceed(request); originalResponse = chain.proceed(request);
...@@ -59,7 +62,7 @@ public class RequestLogInterceptor implements Interceptor { ...@@ -59,7 +62,7 @@ public class RequestLogInterceptor implements Interceptor {
NetLogUtils.debugInfo("Http Error: " + e); NetLogUtils.debugInfo("Http Error: " + e);
throw e; throw e;
} }
long t2 = System.nanoTime() ; long t2 = System.nanoTime();
ResponseBody responseBody = originalResponse.body(); ResponseBody responseBody = originalResponse.body();
...@@ -67,12 +70,12 @@ public class RequestLogInterceptor implements Interceptor { ...@@ -67,12 +70,12 @@ public class RequestLogInterceptor implements Interceptor {
String bodyString = null; String bodyString = null;
if (responseBody != null && isParseable(responseBody.contentType())) { if (responseBody != null && isParseable(responseBody.contentType())) {
bodyString = printResult(request, originalResponse); bodyString = printResult(request, originalResponse);
if (mRequestHandler != null){ if (mRequestHandler != null) {
mRequestHandler.onHttpResultResponse(bodyString, chain, originalResponse); mRequestHandler.onHttpResultResponse(bodyString, chain, originalResponse);
} }
} }
if(mIsDebug) { if (mIsDebug) {
final List<String> segmentList = request.url().encodedPathSegments(); final List<String> segmentList = request.url().encodedPathSegments();
final String header = originalResponse.headers().toString(); final String header = originalResponse.headers().toString();
final int code = originalResponse.code(); final int code = originalResponse.code();
...@@ -87,20 +90,20 @@ public class RequestLogInterceptor implements Interceptor { ...@@ -87,20 +90,20 @@ public class RequestLogInterceptor implements Interceptor {
mPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1), mPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
isSuccessful, code, header, segmentList, message, url); isSuccessful, code, header, segmentList, message, url);
} }
}else { } else {
int code = originalResponse.code(); int code = originalResponse.code();
// 接口返回错误的情况下,埋点告诉服务器原因 // 接口返回错误的情况下,埋点告诉服务器原因
if (code!=200){ if (code != 200) {
String params = ""; String params = "";
Request rq = originalResponse.request(); Request rq = originalResponse.request();
if (rq.method().equals("GET")){ if (rq.method().equals("GET")) {
params = rq.url().query(); params = rq.url().query();
}else if (rq.method().equals("POST")){ } else if (rq.method().equals("POST")) {
params = parseParams(rq); params = parseParams(rq);
} }
String message = originalResponse.message(); String message = originalResponse.message();
ApiErrorCountUtils.Companion.baiDuCount("pardId-ydl_user_error_business","error_log",params,message); ApiErrorCountUtils.Companion.baiDuCount("pardId-ydl_user_error_business", "error_log", params, message);
AliYunLogHelper.Companion.getInstance().sendLog(AliYunLogConfig.API, "error_log--- params:" + params + " ---- message:" + message);
} }
} }
return originalResponse; return originalResponse;
...@@ -109,8 +112,8 @@ public class RequestLogInterceptor implements Interceptor { ...@@ -109,8 +112,8 @@ public class RequestLogInterceptor implements Interceptor {
/** /**
* 打印响应结果 * 打印响应结果
* *
* @param request {@link Request} * @param request {@link Request}
* @param response {@link Response} * @param response {@link Response}
* @return 解析后的响应结果 * @return 解析后的响应结果
* @throws IOException * @throws IOException
*/ */
......
package com.ydl.ydlcommon.utils.log package com.ydl.ydlnet.utils.log
object AliYunLogConfig { object AliYunLogConfig {
/**接口*/ /**接口*/
...@@ -16,6 +16,15 @@ object AliYunLogConfig { ...@@ -16,6 +16,15 @@ object AliYunLogConfig {
/**支付*/ /**支付*/
const val PAY = "Pay" const val PAY = "Pay"
/**云信*/
const val YUNXIN = "YunXin"
/**RTM*/
const val RTM = "Rtm"
/**网络状态*/
const val NETSTATUS = "NetStatus"
/**其他*/ /**其他*/
const val OTHER = "Other" const val OTHER = "Other"
......
package com.ydl.ydlcommon.utils.log package com.ydl.ydlnet.utils.log
import android.util.Log import android.util.Log
import com.aliyun.sls.android.producer.* import com.aliyun.sls.android.producer.*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment