Skip to content

Instantly share code, notes, and snippets.

@leeyc09
Last active May 2, 2017 07:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leeyc09/94f804741159865648c506859c53136a to your computer and use it in GitHub Desktop.
Save leeyc09/94f804741159865648c506859c53136a to your computer and use it in GitHub Desktop.
Custom error Check Interceptor
package net.stylemilk.app.repository.api;
import android.util.Log;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.MalformedJsonException;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.HttpHeaders;
import okio.Buffer;
import okio.BufferedSource;
/**
* Created by lyc on 2016-12-14.
*/
public class PostCheckInterceptor implements Interceptor {
public static final String TAG = PostCheckInterceptor.class.getSimpleName();
private static final Charset UTF8 = Charset.forName("UTF-8");
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
throw e;
}
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
if (HttpHeaders.hasBody(response) && !bodyEncoded(response.headers())) {
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
return response;
}
}
if (!isPlaintext(buffer)) {
return response;
}
if (contentLength != 0) {
Log.d(TAG, buffer.clone().readString(charset));
JsonParser jsonParser = new JsonParser();
try {
JsonObject jsonObject = (JsonObject) jsonParser.parse(buffer.clone().readString(charset));
if(jsonObject.get("result") != null) {
if (!jsonObject.get("result").getAsBoolean()) {
if(jsonObject.get("msg") != null) {
throw new IOException(jsonObject.get("msg").getAsString());
}else{
return response;
}
}
}
} catch (ClassCastException cce){
Log.d(TAG, "ClassCastException");
} catch (MalformedJsonException e) {
Log.d(TAG, "MalformedJsonException");
} catch (NullPointerException e) {
Log.d(TAG, "NullPointerException");
}
}
}
return response;
}
static boolean isPlaintext(Buffer buffer) {
try {
Buffer prefix = new Buffer();
long byteCount = buffer.size() < 64 ? buffer.size() : 64;
buffer.copyTo(prefix, 0, byteCount);
for (int i = 0; i < 16; i++) {
if (prefix.exhausted()) {
break;
}
int codePoint = prefix.readUtf8CodePoint();
if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
return false;
}
}
return true;
} catch (EOFException e) {
return false; // Truncated UTF-8 sequence.
}
}
private boolean bodyEncoded(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment