Skip to content

Commit 49a7f6c

Browse files
author
Vens Chen
committed
update
1 parent 9ac535d commit 49a7f6c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

app/src/main/java/com/threetree/thttp/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected void onCreate(Bundle savedInstanceState)
2626
.baseUrl("your baseUrl")
2727
.addInterceptor(new HttpInterceptor())
2828
.serviceClass(ApiService.class)
29+
.debug(true)
2930
.create();
3031

3132
mDocPresenter = new DocPresenter(this,this);

tthttp/src/main/java/com/threetree/tthttp/RetrofitManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.text.TextUtils;
44

5+
import com.threetree.tthttp.interceptor.LogInterceptor;
6+
57
import java.util.concurrent.TimeUnit;
68

79
import okhttp3.Interceptor;
@@ -27,6 +29,7 @@ public class RetrofitManager<T> {
2729
private long mReadTimeout;
2830
private long mWriteTimeout;
2931
private boolean isReConnect;
32+
private boolean isDebug;
3033

3134
private RetrofitManager()
3235
{
@@ -106,13 +109,25 @@ public RetrofitManager retryOnConnectionFailure(boolean reConnect)
106109
return this;
107110
}
108111

112+
public RetrofitManager debug(boolean debug)
113+
{
114+
isDebug = debug;
115+
return this;
116+
}
117+
118+
public boolean isDebug()
119+
{
120+
return isDebug;
121+
}
122+
109123
public void create()
110124
{
111125
if(TextUtils.isEmpty(mBaseUrl))
112126
throw new NullPointerException("baseUrl must not be null");
113127
if(mServiceClass == null)
114128
throw new NullPointerException("service must not be null");
115129

130+
mClientBuilder.addInterceptor(new LogInterceptor());
116131
mClientBuilder.connectTimeout(mConnectTimeout, TimeUnit.SECONDS);//超时
117132
mClientBuilder.readTimeout(mReadTimeout, TimeUnit.SECONDS);
118133
mClientBuilder.writeTimeout(mWriteTimeout, TimeUnit.SECONDS);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.threetree.tthttp.interceptor;
2+
3+
import android.util.Log;
4+
5+
import com.threetree.tthttp.RetrofitManager;
6+
7+
import java.io.IOException;
8+
9+
import okhttp3.Interceptor;
10+
import okhttp3.Response;
11+
12+
/**
13+
* Created by Administrator on 2018/8/29.
14+
*/
15+
16+
public class LogInterceptor implements Interceptor {
17+
@Override
18+
public Response intercept(Chain chain) throws IOException
19+
{
20+
Response originalResponse = chain.proceed(chain.request());
21+
if(RetrofitManager.getInstence().isDebug())
22+
Log.d("response",originalResponse.body().string());
23+
return originalResponse;
24+
}
25+
}

0 commit comments

Comments
 (0)