Skip to content

Commit

Permalink
修复log response造成的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Mar 2, 2016
1 parent e27c2a2 commit 1f01679
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
或者

```
compile 'com.zhy:okhttputils:2.3.3'
compile 'com.zhy:okhttputils:2.3.4'
```

* Eclipse

下载最新jar:[okhttputils-2_3_2.jar](okhttputils-2_3_2.jar?raw=true)
下载最新jar:[okhttputils-2_3_4.jar](okhttputils-2_3_4.jar?raw=true)

注:需要同时导入okhttp和okio的jar,下载见:[https://github.com/square/okhttp](https://github.com/square/okhttp).

Expand Down
Binary file renamed okhttputils_2_3_2.jar → okhttputils-2_3_4.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions okhttputils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "2.3.3"
version = "2.3.4"

android {
compileSdkVersion 23
Expand Down Expand Up @@ -123,7 +123,7 @@ task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'okhttputils_2_3_2.jar')
rename ('classes.jar', 'okhttputils-2_3_4.jar')
}

makeJar.dependsOn(clearJar, build)
Expand Down
4 changes: 3 additions & 1 deletion okhttputils/okhttputils.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":okhttputils" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="2.3.3" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":okhttputils" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="2.3.4" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down Expand Up @@ -88,6 +88,8 @@
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform (1)" jdkType="Android SDK" />
Expand Down
15 changes: 14 additions & 1 deletion okhttputils/src/main/java/com/zhy/http/okhttp/OkHttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@ public boolean verify(String hostname, SSLSession session)

public OkHttpUtils debug(String tag)
{
mOkHttpClient = getOkHttpClient().newBuilder().addInterceptor(new LoggerInterceptor(tag)).build();
mOkHttpClient = getOkHttpClient().newBuilder().addInterceptor(new LoggerInterceptor(tag, false)).build();
return this;
}

/**
* showResponse may cause error, but you can try .
*
* @param tag
* @param showResponse
* @return
*/
public OkHttpUtils debug(String tag, boolean showResponse)
{
mOkHttpClient = getOkHttpClient().newBuilder().addInterceptor(new LoggerInterceptor(tag, showResponse)).build();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,35 @@ public class LoggerInterceptor implements Interceptor
{
public static final String TAG = "OkHttpUtils";
private String tag;
private boolean showResponse;

public LoggerInterceptor(String tag)
public LoggerInterceptor(String tag, boolean showResponse)
{
if (TextUtils.isEmpty(tag))
{
tag = TAG;
}
this.showResponse = showResponse;
this.tag = tag;
}

public LoggerInterceptor(String tag)
{
this(tag, false);
}

@Override
public Response intercept(Chain chain) throws IOException
{
Request request = chain.request();
logForRequest(request);
Response response = chain.proceed(request);
logForResponse(response);
return response;


return logForResponse(response);
}

private void logForResponse(Response response)
private Response logForResponse(Response response)
{
try
{
Expand All @@ -55,30 +62,38 @@ private void logForResponse(Response response)
Log.e(tag, "protocol : " + clone.protocol());
if (!TextUtils.isEmpty(clone.message()))
Log.e(tag, "message : " + clone.message());
ResponseBody body = clone.body();
if (body != null)

if (showResponse)
{
MediaType mediaType = body.contentType();
if (mediaType != null)
ResponseBody body = clone.body();
if (body != null)
{
if (isText(mediaType))
MediaType mediaType = body.contentType();
if (mediaType != null)
{
Log.e(tag, "responseBody's contentType : " + mediaType.toString());
if (isText(mediaType))
{
Log.e(tag, "responseBody's content : " + body.string());
String resp = body.string();
Log.e(tag, "responseBody's content : " + resp);

body = ResponseBody.create(mediaType, resp);
return response.newBuilder().body(body).build();
} else
{
Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!");
}
}
}
}

Log.e(tag, "========response'log=======end");
} catch (Exception e)
{
// e.printStackTrace();
}

return response;
}

private void logForRequest(Request request)
Expand Down
3 changes: 1 addition & 2 deletions sample-okhttp/sample-okhttp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.zhy/okhttputils/2.3.3/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
Expand All @@ -92,11 +91,11 @@
<orderEntry type="jdk" jdkName="Android API 22 Platform (3)" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="gson-2.3.1" level="project" />
<orderEntry type="library" exported="" name="okhttputils-2.3.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="okhttp-3.2.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="module" module-name="okhttputils" exported="" />
</component>
</module>

0 comments on commit 1f01679

Please sign in to comment.