简体   繁体   中英

Sencha Touch Apk and NetworkOnMainThreadException

I developed a Sencha Touch App that uses network connections to call different web services during login process. I build native apk with Sencha CMD and it works right in Android 2.3.7 but not in newer versions. I read that this is a NetworkOnMainThreadException problem. The solutions I found for Android are:

1) to make webservice calls by implementing AsyncTask.

2) use StrictMode Class in Android and set the policy

But how can apply these solutions in my Sencha Javascript App? I really hope that someone of you had found a solution!

Thanks for any advice!

Edit

After some tests I discover that my app works fine in android 2.3.7 but not in android 4 because of one of my webservice. It's a liferay json web service. It's the fourth web service that i call and if i don't call it my app works fine. Does someone of you know if the Android (versions >2.3.7) Web View doesn't support Liferay Web Service?

I think You're performing network operations on UI Thread . This will cause blocking User Interface. Since Android 3.0, performing network operations on UI Thread is not applicable and it throws NetworkOnMainThreadException .

In the API document, it says:

Network operations can involve unpredictable delays. To prevent this from causing a poor user experience, always perform network operations on a separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread.

So you should use AsyncTask in order to handle this problem.

Check this out link . This can be a sample for writing AsyncTask

If you wonder why this procedure is applied and why this exception is thrown. Check this out as well.

Hope this may help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM