I am developing a restlet server inside a Glassfish server that will receive petitions from an Android client.
Based on the Hello World application and on the Object serialization tutorial
Here’s some code:
Server Side
MyUser.java
package com.server.common;
import java.io.Serializable;
public class MyUser implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
public MyUser() {}
public MyUser(String username) {
super();
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
UserResource.java
package com.server.common;
import org.restlet.resource.Get;
public interface UserResource {
@Get
public MyUser getUser();
}
UserServerResource.java
package com.server;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import com.server.common.MyUser;
import com.server.common.UserResource;
public class UserServerResource extends ServerResource implements UserResource {
@Get
public MyUser getUser() {
MyUser u = new MyUser("Nickname from server");
return u;
}
}
Client Side
-
The same UserResource.java and MyUser.java inside the package
com.client.common -
MainActivity.java extract
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ClientResource cr = new ClientResource( "http://MYSERVERIP:8080/FamilyWeb/username"); resource = cr.wrap(UserResource.class); try { MyUser u = resource.getUser(); Log.w("success",u.toString()); } catch (Exception e) { e.printStackTrace(); }
LogCat
09-29 22:31:52.650: W/System.err(1409): java.lang.NullPointerException
09-29 22:31:52.650: W/System.err(1409): at com.dimunoz.family.tablet.MainActivity.onCreate(MainActivity.java:33)
09-29 22:31:52.650: W/System.err(1409): at android.app.Activity.performCreate(Activity.java:4465)
09-29 22:31:52.650: W/System.err(1409): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-29 22:31:52.660: W/System.err(1409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-29 22:31:52.660: W/System.err(1409): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-29 22:31:52.660: W/System.err(1409): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-29 22:31:52.660: W/System.err(1409): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-29 22:31:52.660: W/System.err(1409): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 22:31:52.660: W/System.err(1409): at android.os.Looper.loop(Looper.java:137)
09-29 22:31:52.660: W/System.err(1409): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-29 22:31:52.660: W/System.err(1409): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 22:31:52.660: W/System.err(1409): at java.lang.reflect.Method.invoke(Method.java:511)
09-29 22:31:52.660: W/System.err(1409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 22:31:52.660: W/System.err(1409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 22:31:52.660: W/System.err(1409): at dalvik.system.NativeStart.main(Native Method)
If I change the getUser() method to return a String object and make the respective changes, my Android client gets the response, but if I run the code shown above, I get a NullPointerException.
What am I missing?
The problem was that the name of the common files packages was different in the server and the client, so the solution was to rename them to the same name.
In the Restlet Mailing List one of the moderators, Thierry Boileau, answered me why they should have the same name: