Created
October 29, 2015 12:51
-
-
Save voghDev/0bd7050a675ee0f31e51 to your computer and use it in GitHub Desktop.
retrofit2 interceptor that logs the raw JSON response from API. After that it clones it and returns a sane copy that other classes can consume.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
import com.appandweb.prjtestdrivendev.BuildConfig; | |
import com.squareup.okhttp.Interceptor; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import com.squareup.okhttp.ResponseBody; | |
import java.io.IOException; | |
public class LogJsonInterceptor implements Interceptor { | |
@Override | |
public Response intercept(Interceptor.Chain chain) throws IOException { | |
Request request = chain.request(); | |
Response response = chain.proceed(request); | |
String rawJson = response.body().string(); | |
Log.d(BuildConfig.APPLICATION_ID, String.format("raw JSON response is: %s", rawJson)); | |
// Re-create the response before returning it because body can be read only once | |
return response.newBuilder() | |
.body(ResponseBody.create(response.body().contentType(), rawJson)).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you