Skip to content

Commit 604637a

Browse files
committed
fix: bring back allowLargeResponse when not using useLegacy
1 parent 1fb44fd commit 604637a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/https.android.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,14 @@ export function createRequest(
544544
const tag = `okhttp_request_${CALL_ID++}`;
545545
const call = client.newCall(request.tag(tag).build());
546546

547+
// We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException.
548+
// Note that it would probably be better to offload it to a Worker or (natively running) AsyncTask.
549+
// Also note that once set, this policy remains active until the app is killed.
550+
if (opts.useLegacy === false && opts.allowLargeResponse) {
551+
android.os.StrictMode.setThreadPolicy(
552+
android.os.StrictMode.ThreadPolicy.LAX
553+
);
554+
}
547555
return {
548556
nativeRequest: call,
549557
cancel: () => cancelRequest(tag, client),

src/https.common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export interface HttpsRequestOptions extends HttpRequestOptions {
4545
*/
4646
timeout?: number;
4747

48+
/**
49+
* On Android large responses may crash the app (fi. https://httpbin.org/bytes/10000).
50+
* By setting this to true and when not using useLegacy, we allow large responses on the main thread (which this plugin currently does).
51+
* Note that once set to true, this policy remains active until the app is killed.
52+
*/
53+
allowLargeResponse?: boolean;
54+
4855
/**
4956
* iOS for now
5057
*/

0 commit comments

Comments
 (0)