So I am new with restlet. I am creating a Android application that can communicate with a GAE server (with objectify DB)
I Did this very good tutorial to learn:
http://www.tutos-android.com/webservice-rest-android-appengine-restlet-objectify
It’s working very well but do very little.
Onely 2 methods:
public interface UserControllerInterface {
@Put
void create(User user);
@Get
Container getAllUsers();
}
For my application its more complicated so I add many more methods:
public interface UserControllerInterface {
@Put
public void createUser(ObagooUser user);
@Put
public void createMessage(ObagooUser user, String message);
@Put
public void updateMessage(ObagooMessage message);
@Get
public List<ObagooUser> getAllUser();
@Get
public ObagooUser getUserById(String id);
@Get
public List<ObagooMessage> getAllMessage();
@Get
public List<ObagooMessage> getAllMessageFromSender(ObagooUser sender);
@Get
public ObagooMessage getFreeMessage(ObagooUser user);
}
Each of these mothds working server side (I tested with Junit).
Now I am coding the android part and I am having problems.
When I do a simple call to getAllMessage() I get an error:
java.lang.IllegalArgumentException: id cannot be zero
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:44)
at com.googlecode.objectify.ObjectifyFactory.typedKeyToRawKey(ObjectifyFactory.java:269)
at com.googlecode.objectify.impl.ObjectifyImpl.find(ObjectifyImpl.java:159)
at com.googlecode.objectify.impl.ObjectifyImpl.find(ObjectifyImpl.java:183)
at com.obagoo.dao.ObagooUserDAO.getUserById(ObagooUserDAO.java:43)
at com.obagoo.controller.ObagooController.getUserById(ObagooController.java:47)
It’s going in the wrong method (getUserById).
I put a break point in my getAllMessage and it’s going in, but it is also going in other methods.
If I test many times, sometimes it’s calling, createUser or another random method.
Do you see what I am doind wrong?
Adding the getAllMessage code:
public List<ObagooMessage> getAllMessage() {
// logger.debug("Getting all Obagoo Messages");
List<ObagooMessage> msg = new ArrayList<ObagooMessage>();
Objectify ofy = ObjectifyService.begin();
Query<ObagooMessage> q = ofy.query(ObagooMessage.class);
for (ObagooMessage u : q) {
msg.add(u);
}
return msg;
}
In the examples that I’ve seen, its always shown that you should separate the
controller/resourcehandling theURIfor the list resource from the single item (id/namebased) resource. So you would have something like:Notice the plural naming on the first class:
UsersController, and singular naming on the the second class:UserController. The first class would handle cases where no id was being provided, such as a get of all users. Also, note when the id is provided in theURI, it can be automatically mapped into an id field on the class. So theGetmethod has no parameters on the method call.As for handling a subset, then for messages from a specific user, that could be handled with query parameters. For instance when calling via a
URIwith/messages?sender=id, theMessagesController.classwould use the following in the method handling the Get:Hope that helps. I’m no expert, so anyone feel free to correct me.