Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid use of internal OkHttp API #7

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand All @@ -28,7 +29,6 @@
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSource;

Expand Down Expand Up @@ -62,7 +62,7 @@ public Response intercept(@NonNull Chain chain) throws IOException {

if (isArray || pageJson != null) {
MediaType contentType = response.body().contentType();
Charset charset = contentType != null ? contentType.charset(Util.UTF_8) : Util.UTF_8;
Charset charset = contentType != null ? contentType.charset(Charset.defaultCharset()) : Charset.defaultCharset();
BufferedSource source = response.body().source();
Buffer newResponse = new Buffer();

Expand All @@ -81,12 +81,23 @@ public Response intercept(@NonNull Chain chain) throws IOException {
newResponse.writeString("," + pageJson + "}", charset);
}
} finally {
Util.closeQuietly(source);
closeQuietly(source);
}
ResponseBody newBody = ResponseBody.create(contentType, newResponse.size(), newResponse);
return response.newBuilder().body(newBody).build();
}
}
return response;
}

private void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
}