I have searched everywhere for this, and I cannot find the answer. I even saw a copy of the book Restlet, which gives only a partial answer and even that incomplete answer is incorrect.
What I'm trying to do is very simple. I need to make a simple GET request for an HTTP url. I know how to do this synchronously:
Engine.getInstance().getRegisteredClients().clear(); Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null)); ClientResource resource=new ClientResource(url); Representation rep=resource.get(); String respText=rep.getText();
Of course, the problem is that this block is on resource.get () until a response is received. I really want to do this asynchronously, i.e. Set a callback (using the resource.setOnResponse? Method), and then disconnect the request without blocking. I would also like to set a timeout value so that if I don't get a timeout within a reasonable amount of time, it will trigger some kind of onTimeout or onError method.
You might think that this is a very common thing that someone might want to do with Restlet, but I can't find the documentation that discusses this. The only discussion I see is in the Restlet book, which in Listing 9.2 says that the get () method does not block when it actually happens. In other words, I tried this:
Engine.getInstance().getRegisteredClients().clear(); Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null)); ClientResource resource=new ClientResource(url); resource.setOnResponse(new Uniform() { public void handle(Request request, Response response) { try { int statusCode=response.getStatus().getCode(); // Print status code, should be 200 System.out.println("Status code is "+statusCode); System.out.println(""); System.out.println(""); if (statusCode==200) { onSuccess(response); // this is my own success handler method } else { System.out.println("ERROR: Bad response from server"); } } catch (Exception ex) { // handle exception } } }); System.out.println("Before resource get"); resource.get(); // This blocks!!
Can someone please show me how to do this? Thanks.
source share