This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

java.lang.ClassNotFoundException: Didn't find class "java.time.LocalDateTime"

 
Ranch Hand
Posts: 120
  • 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, an error
Please help me to find a solution i'm really stuck
 
Saloon Keeper
Posts: 5809
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which android version does the device run on? At least 26 (which is when java.time was added)?
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the api is 28
 
Rancher
Posts: 4270
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim was asking what version the phone is running.
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the version of android iss 6.0 in  the phone
But when i use the emulator api 28 and android 9, another error appear
as the following :
 
Tim Moores
Saloon Keeper
Posts: 5809
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android 6 does not support java.time.
 
Dave Tolls
Rancher
Posts: 4270
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you need java.time, the minSdkVersion should presumably be 26?
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i change minsdk to 26 and i use an emulator android 9 , but still get as the following error
 
Saloon Keeper
Posts: 10657
227
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it looks like you are using LocalDateTime in a class that is supposed to be serialized to JSON. This doesn't have anything to do with whether the phone supports the java.time API. Your application just doesn't know how to serialize LocalDateTime.

Note that using LocalDateTime in server-side APIs is usually a bad idea. LocalDateTime is splendid inside an application instance, but when communicating with external applications or databases, you want the data to represent an unambiguous instant in time. That's why you should be using either ZonedDateTime or Instant.

Maybe changing your server-side API to use ZonedDateTime or Instant fixes your problem. Otherwise, if you generated the client using some tool, using the latest version of the tool could solve the problem. If it doesn't, then you have to explicitly provide an adapter to Moshi that serializes LocalDateTime.

As a final thought, I see that the field that causes the issue is UserData.birthDay. Why are you using a LocalDateTime for a birthday at all? Use LocalDate and sidestep the issue completely.
 
Tim Moores
Saloon Keeper
Posts: 5809
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is quite specific about what needs to be done; have you investigated what it's telling you? It seems LocalDateTime needs extra setup if used with that JSON library.
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Maybe changing your server-side API to use ZonedDateTime or Instant fixes your problem. Otherwise, if you generated the client using some tool, using the latest version of the tool could solve the problem. If it doesn't, then you have to explicitly provide an adapter to Moshi that serializes LocalDateTime.



the following code is serializer, i can't change any thing in the model UsersData because i genetrated with swagger tools
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same error appear if i change to ZonedDateTime or DateTime
 
Stephan van Hulst
Saloon Keeper
Posts: 10657
227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of the Date adapter. If your application uses only java.time classes, you shouldn't need it. Use LocalDate for the birthday, and write your own adapter to (de)serialize a LocalDate. You can do this by extending com.squareup.moshi.JsonAdapter<LocalDate>.
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create this adapter and where should i put it in my app folder
 
Stephan van Hulst
Saloon Keeper
Posts: 10657
227
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just create a class that extends JsonAdapter and implements the toJson() and fromJson() methods. You should not put it in your application, but instead in a separate library that you can include as a dependency in both your server and your client applications.
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, i will try
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following the class LocalDateTimeAdapter
 
Stephan van Hulst
Saloon Keeper
Posts: 10657
227
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, why are you using LocalDateTime, and not just LocalDate? Are you expecting users to know the exact time they were born? Does the application do anything with that knowledge?

It's simple. In the toJson() method, just format the LocalDate as a string as write it to the writer. In the fromJson() method, read a string from the reader, and parse it as a LocalDate.
 
Maha Sakka
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should use LocalDateTime because in the data class (model) i can't change the type of attribute
the following code for model UsersData:


when i use the custom class LocalDateTimeAdapter,
in a serializer as the following

the following error appear :
 
Maha Sakka
Ranch Hand
Posts: 120
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolve the problem finally, thank you for all
The best forum really


i just change the DateTimeFormatter.ISO_LOCAL_DATE to DateTimeFormatter.ISO_DATE_TIME
and it works finally
 
That is a really big piece of pie for such a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!