0

Alright so I've got this Android Studio app with user class

public class User{
   int age;
   String name;
   int yearOfBirth;
}

And then I've got this two methods

public static Object fromString(String s) throws IOException,ClassNotFoundException {
    byte [] data = Base64.getDecoder().decode(s);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o  = ois.readObject();
    ois.close();
    return o;
}

public static String toString(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    return Base64.getEncoder().encodeToString(baos.toByteArray());
}

So I create User object, convert it to String, save it to database on other device where I need to convert the object back to User object to change some stuff, convert to String again save to database and then be able to decode the User object in my app again.

Problem is when trying to decode the String on the server side I'm getting this exception

Exception in thread "main" java.lang.ClassNotFoundException: com.example.mikithenics.User

What could be the possible solution ? Any help is appreciated.

Martin Lukas
  • 314
  • 5
  • 17
  • "What could be the possible solution ?" -- well, the best solution by far is to stop using `ObjectInputStream` and `ObjectOutputStream`. Use JSON, XML, or something else for a neutral, environment-independent encoding of the data. In particular, using object serialization for cross-VM data exchange (Android's Dalvik or ART, vs. the JVM on your server) is really not a good plan. Beyond that, though, your server needs that class if you intend to deserialize `User` objects on the server. – CommonsWare May 05 '20 at 22:45
  • Yes I know that it needs that class "com.example.mikithenics." this part is the problem. Can I decode and encode entire object into JSON ? – Martin Lukas May 05 '20 at 22:48
  • The "O" in JSON stands for "object" (JavaScript Object Notation). `{ "age": 19, "name": "Martin Lukas", "yearOfBirth": 2001 }` would be one JSON representation of a `User` object. – CommonsWare May 05 '20 at 23:00
  • Yeah I know how does JSON look like and what it means, though I dunno how to convert Java object into JSON the actual User class has like 50 variables and I can only think of assigning it one by one User.name= JSON.getString("name"); etc. – Martin Lukas May 05 '20 at 23:09
  • There are libraries for JSON parsing and creation, such as [Moshi](https://github.com/square/moshi). – CommonsWare May 05 '20 at 23:29

1 Answers1

1

You can serialize this using Google Gson lib like this:

Gson gson = new Gson();

String jsonString = gson.toJson(userObject);

And deserialize like this:


Gson gson = new Gson();

User userObject = gson.fromJson(userJsonString, User.class);

To use Gson lib, put this in your app build.gradle file, inside dependencies:

implementation 'com.google.code.gson:gson:2.8.6'

RennanP
  • 68
  • 1
  • 4