Question

I'm looking for a way to call methods from my android client (written in Java) in the server (instance of amazon ec2 written in Java). I'm looking for something like play framework, where I could write a GET request with a method name (lets say, calculateHighScore) and in the routes.config I set the get method to execute calculateHighScore method from the server.

I have read about volley and its way of communication by JSON, but still I dont understand what should I write in the server side to execute a specific method and return an appropriate response.

Was it helpful?

Solution

You could try to use Jersey RESTFul Services library.

For example on your server you could have some code like this:

@Path("/your_class")
public class YourClass {
    [...]
    @POST
    @Path("/your_method")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Object> yourMethod(String input){
        [...]
        return new ArrayList<Object>();
    }
}

And on the client side you could have some code like this:

[...]
ServiceFinder.setIteratorProvider(new AndroidServiceIteratorProvider());
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
clientConfig.getClasses().add(JacksonJsonProvider.class);
Client client = Client.create(clientConfig);

WebResource webResource = client.resource("http://yoursite.net:8080/NameOfService/rest/your_class/your_method");
ClientResponse response = webResource.accept("application/json").post(ClientResponse.class,"your input");
ArrayList<Object> list = response.getEntity(new GenericType<ArrayList<Object>>() {});
[...]

Also this is a good tutorial to help you with Jersey and its libraries.

OTHER TIPS

Design a API that handles responses to GET and POST requests from your android client. Api has to handle all the queries from the the android client.

For example if you want your overall High Score the client will send a request like this :

test.com/api/?m="calculateHighScore"

Now your api will extract parameters from your url and respond by sending a response with High Score.

You can read more about API Design here. And more here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top