我正在 Glassfish 服务器内开发一个 restlet 服务器,它将接收来自 Android 客户端的请求。
这是一些代码:
服务器端
我的用户.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;
}
}
用户资源.java
package com.server.common;
import org.restlet.resource.Get;
public interface UserResource {
@Get
public MyUser getUser();
}
用户服务器资源.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;
}
}
客户端
包内相同的 UserResource.java 和 MyUser.java
com.client.common
MainActivity.java 提取
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(); }
日志猫
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)
如果我更改 getUser() 方法以返回 String 对象并进行相应更改,我的 Android 客户端会收到响应,但如果我运行上面显示的代码,我会收到 NullPointerException。
我错过了什么?