• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

“NetworkOnMainThreadException” in the android Kollin app which use file generated by swagger

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in Kotlin, i want to create an android mobile app.I use Android studio 3.3,the Android API level 28. I have used a swagger to get all my RESTful web service, thus, i have the android-client-generated the file that contains all the web service. As a start, I created an android interface for authentication with Email and Password". Among the web service available, "apiMobileUsersGetByFireBaseIDGet". This Ws, use the firebase Id to authenticate.
I create a kotlin class AUthentication as the following :


i added alse the internet permission in the AndroidManifest.xml. My problem is when i run my app in my mobile phone, a "NetworkOnMainThreadException" sets off.I tried for several times to correct this error.I'm really stuck. The error description as the following :

 
Saloon Keeper
Posts: 2614
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are performing a network operation in a UI thread which could lead to the UI becoming unresponsive.  Android throws a NetworkOnMainThreadException to remind you of this.

Try running the authenticator code in a AsyncTask.
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change my code which i use the coroutine, as the following:
       
Another error appears as the following :

could you please telle me where's the problem and how can i correct them

 
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.time package is available on Android only on API 26 and up; see https://developer.android.com/reference/java/time/package-summary. What API version are you targeting?
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Api 28 ,
Here is an excerpt from my file:
 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
minSdkVersion is 23, so the app can be run on devices that don't have that package. You need to ensure that the code calling java.time is not executed on those devices.
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run my app in my phone which the android version is 6.0 and Api 23
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in the file generated by swagger an object Serializer that use moshi library as the following :

and when i click in Date::class.java, which is a class Java , the following lines :

gives an error, "Cannot resolve symbol" , how can i correct it
 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that generates code that uses "sun.*" classes then it is VERY old. Replace it with something newer. Java has had time and calendar classes built in for a long time. If you can't use java.time (which on Android 6 you can't) then use java.util. Calendar.
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to thank you for your reply first of all.
The date.class  is deprecated, how can i change it

 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Date class works fine, there is no pressing need to replace it. Unless you need to consider different time zones (which Date ignores) - do you?

If so, java.util.Calendar and friends can be used. There are many tutorials available for it.
 
Maha Sakka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is how can i correct this exception, i lost a lot of time by searching, could you please help me
 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already mentioned that you can't use java.time on Android below API 26. If you're unfamiliar with Date and/or Calendar, then there is no shortage of tutorials for those out there on the web. If you have a specific question about them, start a new topic in the Java in General forum, as that has nothing to do with Android.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!