Java – How to import the android.os.bundle format in a normal Java project?

How to import the android.os.bundle format in a normal Java project?… here is a solution to the problem.

How to import the android.os.bundle format in a normal Java project?

I’m trying to send data in android.os.bundle format from an Android emulator to a PC server via socket. Now I can get the object from the ObjectInputStream on the PC server side, but I can’t convert it to bundle format.

Here are some key lines of code I have on the server side of my PC:

android.os.Bundle testInfo = new android.os.Bundle();
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
testInfo = (Bundle)in.readObject();  

This is what the console demo is about:

java.lang.RuntimeException: Stub!
at android.os.Bundle.<init>(Bundle.java:5)
at TCPDesktopServer.run(TCPDesktopServer.java:31)
at java.lang.Thread.run(Unknown Source)

Solution

I’m not sure you can do this so easily.

But even if you can successfully use android.os.bundle in your server, you really shouldn’t because you will be stuck in the Android format that may eventually change.

Aside from being an Android app, it’s actually a matter of decoupling your solution. You can use a neutral format to transfer data over a socket connection. That way, changes to the Android API don’t necessarily lead to changes to your PC server implementation.

I recommend that you use formats such as XML, JSON, or YAML.

In that case, you may need to convert android.os.Bundle to the neutral format you chose in your Android application, but you will benefit from decoupling your solution.

Another benefit is that if tomorrow you need to connect a new app to your server, you won’t have to deal with Android APIs in the new app.

Related Problems and Solutions