Search code examples
javaandroidhttprestlet

How to make Restlet class handle GET, POST, PUT, DELETE requests? Android


I've a HTTP server running on my Android using RESTlet, works fine.

It is handling the URI as I want.

To do this I'm extending my classes to Restlet, I mean something like this:

public class PersonRestlet extends Restlet { (...) }

Again, this is working as expected.

Now I need to handle GET, POST, DELETE, PUT request but as far as I know the Restlet class can not handle it, am I write? I can only see/find the public void handle(Request request, Response response).

From what I understood from examples and reading online I would need to extend ServerResources class and then use the @Get, @Put, etc to handle the GET, PUT, etc.

So my question is, how can I handle GET, POST, PUT, DELETE requests just extending the Restlet class? Or a workaround would be nice as well.

PS; I know I can extend ServerResources instead of Restlet but I must avoid, at all cost, that due to the way the app is structured and working.


Solution

  • This is how I'm doing it, my workaround:

    @Override
    public void handle(Request request, Response response) {
    
        String type = request.getMethod().getName();
        String uid= (String) request.getAttributes().get("uid");
    
        if(type.equalsIgnoreCase("get"))
        {
              (do you processing here)
        }
    }