From f148dea081057eccee739965e756f70278ed6c63 Mon Sep 17 00:00:00 2001 From: Vaibhav Agrawal Date: Fri, 24 Aug 2018 12:35:41 +0530 Subject: [PATCH] Include requestBody in requestStats --- app/build.gradle | 19 +++++++-------- .../okhttpstatsdemo/OnResponseReceived.java | 3 +++ build.gradle | 4 +++- gradle/wrapper/gradle-wrapper.properties | 4 ++-- library/build.gradle | 23 ++++++++++--------- .../interpreter/DefaultInterpreter.java | 13 ++++++++--- .../okhttpstats/model/RequestStats.java | 17 ++++++++++++++ .../reporter/NetworkEventReporter.java | 4 ++++ .../reporter/NetworkEventReporterImpl.java | 3 +++ 9 files changed, 64 insertions(+), 26 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9bb9c56..3d1e0cd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,14 +10,14 @@ allprojects { apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 27 + buildToolsVersion "27.0.3" lintOptions { abortOnError false } defaultConfig { minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" useLibrary 'org.apache.http.legacy' @@ -31,10 +31,11 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.1.0' - compile 'com.android.support:design:25.1.0' - compile 'com.android.volley:volley:1.0.0' - compile project(':library') + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:design:27.1.1' + implementation 'com.android.volley:volley:1.1.1' + implementation 'com.squareup.okhttp3:okhttp:3.11.0' + implementation project(':library') } diff --git a/app/src/main/java/com/flipkart/okhttpstatsdemo/OnResponseReceived.java b/app/src/main/java/com/flipkart/okhttpstatsdemo/OnResponseReceived.java index 22598d8..ab2a9f9 100644 --- a/app/src/main/java/com/flipkart/okhttpstatsdemo/OnResponseReceived.java +++ b/app/src/main/java/com/flipkart/okhttpstatsdemo/OnResponseReceived.java @@ -14,6 +14,7 @@ public void onResponseSuccess(NetworkInfo info, RequestStats requestStats) { + "\nId : " + requestStats.id + "\nUrl : " + requestStats.url + "\nMethod : " + requestStats.methodType + + "\nStatus Code : " + requestStats.statusCode + "\nHost : " + requestStats.hostName + "\nRequest Size : " + requestStats.requestSize + "\nResponse Size : " + requestStats.responseSize @@ -27,6 +28,8 @@ public void onResponseError(NetworkInfo info, RequestStats requestStats, Excepti + "\nId : " + requestStats.id + "\nUrl : " + requestStats.url + "\nMethod : " + requestStats.methodType + + "\nStatus Code : " + requestStats.statusCode + + "\nRequest Body : " + requestStats.requestBody + "\nHost : " + requestStats.hostName + "\nRequest Size : " + requestStats.requestSize + "\nResponse Size : " + requestStats.responseSize diff --git a/build.gradle b/build.gradle index 74b2ab0..55db43f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' + classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +16,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2996c2e..50c92a8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sat Nov 26 09:48:40 IST 2016 +#Fri Aug 24 12:18:43 IST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/library/build.gradle b/library/build.gradle index 9ed0129..e0ad615 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -24,12 +24,13 @@ buildscript { repositories { jcenter() + google() maven { url 'http://repo1.maven.org/maven2' } maven { url 'https://plugins.gradle.org/m2/' } } dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' + classpath 'com.android.tools.build:gradle:3.1.4' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3' } @@ -39,14 +40,14 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' android { - compileSdkVersion 25 - buildToolsVersion '25.0.2' + compileSdkVersion 27 + buildToolsVersion '27.0.3' lintOptions { abortOnError false } defaultConfig { minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" } @@ -59,12 +60,12 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - compile 'com.android.support:support-annotations:25.1.0' - compile 'com.squareup.okhttp3:okhttp:3.4.2' + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation 'com.android.support:support-annotations:27.1.1' + implementation 'com.squareup.okhttp3:okhttp:3.11.0' - testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:1.10.19' - testCompile 'com.squareup.okhttp3:mockwebserver:3.4.2' - testCompile 'org.robolectric:robolectric:3.2.2' + testImplementation 'junit:junit:4.12' + testImplementation 'org.mockito:mockito-core:2.18.0' + testImplementation 'com.squareup.okhttp3:mockwebserver:3.6.0' + testImplementation 'org.robolectric:robolectric:3.3.2' } \ No newline at end of file diff --git a/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java b/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java index 88a79df..0d9c8c2 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java +++ b/library/src/main/java/com/flipkart/okhttpstats/interpreter/DefaultInterpreter.java @@ -39,6 +39,7 @@ import okhttp3.MediaType; import okhttp3.Request; +import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okio.BufferedSource; @@ -60,7 +61,7 @@ public DefaultInterpreter(NetworkEventReporter mEventReporter) { public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) throws IOException { ResponseBody responseBody = response.body(); - final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host()); + final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); //if response does not have content length, using CountingInputStream to read its bytes if (response.header(CONTENT_LENGTH) == null) { @@ -103,7 +104,7 @@ public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, if (Utils.isLoggingEnabled) { Log.d("Error response: ", e.getMessage()); } - final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.header(HOST_NAME)); + final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.header(HOST_NAME), request.body()); mEventReporter.httpExchangeError(okHttpInspectorRequest, e); } @@ -116,13 +117,16 @@ static class OkHttpInspectorRequest implements NetworkEventReporter.InspectorReq final String mMethodType; final long mContentLength; final String mHostName; + final RequestBody mRequestBody; - OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength, String hostName) { + OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength, + String hostName, RequestBody requestBody) { this.mRequestId = requestId; this.mRequestUrl = requestUrl; this.mMethodType = methodType; this.mContentLength = contentLength; this.mHostName = hostName; + this.mRequestBody = requestBody; } @Override @@ -149,6 +153,9 @@ public long requestSize() { public String hostName() { return mHostName; } + + @Override + public RequestBody requestBody() { return mRequestBody; } } /** diff --git a/library/src/main/java/com/flipkart/okhttpstats/model/RequestStats.java b/library/src/main/java/com/flipkart/okhttpstats/model/RequestStats.java index 3260167..5944b44 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/model/RequestStats.java +++ b/library/src/main/java/com/flipkart/okhttpstats/model/RequestStats.java @@ -25,6 +25,7 @@ import java.net.URL; +import okhttp3.RequestBody; import okhttp3.ResponseBody; /** @@ -37,6 +38,7 @@ public class RequestStats { public String methodType; public long requestSize; public long responseSize; + public RequestBody requestBody; public ResponseBody responseBody; public String hostName; public int statusCode; @@ -46,4 +48,19 @@ public class RequestStats { public RequestStats(int requestId) { this.id = requestId; } + + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Id : ").append(id) + .append("\nMethod : ").append(methodType) + .append("\nHost : ").append(hostName) + .append("\nStatusCode : ").append(statusCode) + .append("\nRequest Size : ").append(requestSize) + .append("\nResponse Size : ").append(responseSize) + .append("\nTime Taken : ").append(endTime - startTime) + .append("\nUrl : ").append(url) + .append("\nRequest Body : ").append(requestBody) + .append("\nResponse Body : ").append(responseBody); + return stringBuilder.toString(); + } } \ No newline at end of file diff --git a/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporter.java b/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporter.java index 20ca4ee..81a94e2 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporter.java +++ b/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporter.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.net.URL; +import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; @@ -70,6 +71,9 @@ interface InspectorRequest { long requestSize(); String hostName(); + + @Nullable + RequestBody requestBody(); } interface InspectorResponse { diff --git a/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporterImpl.java b/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporterImpl.java index 9525f76..f582724 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporterImpl.java +++ b/library/src/main/java/com/flipkart/okhttpstats/reporter/NetworkEventReporterImpl.java @@ -59,6 +59,7 @@ public void responseReceived(final InspectorRequest inspectorRequest, final Insp requestStats.url = inspectorRequest.url(); requestStats.methodType = inspectorRequest.method(); requestStats.hostName = inspectorRequest.hostName(); + requestStats.requestBody = inspectorRequest.requestBody(); requestStats.responseSize = inspectorResponse.responseSize(); requestStats.statusCode = inspectorResponse.statusCode(); requestStats.startTime = inspectorResponse.startTime(); @@ -77,6 +78,7 @@ public void httpExchangeError(final InspectorRequest inspectorRequest, final IOE requestStats.methodType = inspectorRequest.method(); requestStats.hostName = inspectorRequest.hostName(); requestStats.requestSize = inspectorRequest.requestSize(); + requestStats.requestBody = inspectorRequest.requestBody(); mNetworkRequestStatsHandler.onHttpExchangeError(requestStats, e); } } @@ -90,6 +92,7 @@ public void responseInputStreamError(final InspectorRequest inspectorRequest, fi requestStats.url = inspectorRequest.url(); requestStats.methodType = inspectorRequest.method(); requestStats.hostName = inspectorRequest.hostName(); + requestStats.requestBody = inspectorRequest.requestBody(); requestStats.statusCode = inspectorResponse.statusCode(); requestStats.startTime = inspectorResponse.startTime(); requestStats.endTime = inspectorResponse.endTime();