nativescript-community / https

Secure HTTP client with SSL pinning for Nativescript - iOS/Android
https://nativescript-community.github.io/https/
Other
50 stars 42 forks source link

Android crashes with NetworkOnMainThreadException on larger responses #7

Closed EddyVerbruggen closed 4 years ago

EddyVerbruggen commented 7 years ago

Has anybody seen a crash on Android with (slightly) larger responses?

Even a call to https://httpbin.org will crash the app, but the smaller https://httpbin.org/get won't.

It's because of the line response.body().string() which runs the main thread and Android detects that as something that can degrade app responsiveness.

I have a fix for this in my fork but won't submit a PR until someone else can confirm it's a real issue instead of the way I use it.

roblav96 commented 7 years ago

@EddyVerbruggen The only crashes I've ever come across was related to the thread scheduler.

Adding this code:

if (ENV != 'PRODUCTION') {
    // for okhttp
    let policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build()
    android.os.StrictMode.setThreadPolicy(policy)
}

resolved it for me.

In regards to large responses, my app GoJobYourself scrapes entire web pages using this plugin and I've never had a problem with it on android or ios.

EddyVerbruggen commented 7 years ago

@roblav96 That bit of code is exactly what I had to add to make it work on Android with a simple GET of https://httpbin.org/.

Why do you only need that when ENV != 'PRODUCTION'? Does Android not throw those errors for release builds perhaps?

roblav96 commented 7 years ago

@EddyVerbruggen I have no idea. lol In my past experiences while testing release builds this was never an issue.

GalushkoVasiliy commented 5 years ago

@EddyVerbruggen How did you solve this problem?

EddyVerbruggen commented 5 years ago

Like I mentioned in this comment: https://github.com/gethuman/nativescript-https/issues/7#issuecomment-289222847

san3k1990 commented 4 years ago

I have same problem. Sometimes i have android.os.NetworkOnMainThreadException, when i use post request.

EddyVerbruggen commented 4 years ago

@san3k1990 Did you try this suggestion? https://github.com/EddyVerbruggen/nativescript-https/tree/99faa4fc5ae2a6363d8ebb4a09dfa945bc0700a0#android-troubleshooting

san3k1990 commented 4 years ago

@EddyVerbruggen Yes, with allowLargeResponse: true the problem is gone. Thanks.