Skip to content

Commit b030b88

Browse files
EddyVerbruggeneddyverbruggen
authored andcommitted
Prevent crashes on Android with larger responses #1
1 parent ed4c1f8 commit b030b88

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

https.android.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

https.android.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ function getClient(reload: boolean = false): okhttp3.OkHttpClient {
163163
return Client
164164
}
165165

166+
// We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException.
167+
// Note that it would be better to offload it to an AsyncTask but that has to run natively to work properly.
168+
// No time for that now, and actually it only concerns the '.string()' call of response.body().string() below.
169+
const strictModeThreadPolicyPermitAll = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build()
170+
166171
export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsResponse> {
167172
return new Promise(function(resolve, reject) {
168173
try {
@@ -205,6 +210,9 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
205210
))
206211
}
207212

213+
// enable our policy
214+
android.os.StrictMode.setThreadPolicy(strictModeThreadPolicyPermitAll)
215+
208216
client.newCall(request.build()).enqueue(new okhttp3.Callback({
209217
onResponse: function(task, response) {
210218
// console.log('onResponse')

0 commit comments

Comments
 (0)