Search code examples
androidpostandroid-annotations

Trouble sending POST parameter with androidannotations


so I'm trying to send a simple String to my REST server from an Android app using androidannotations.

http://localhost:8080/TestServer_RESTJersey/api/lanceurs/parPays

Using Advanced REST client chrome extension, I send the parameter :

country=Europe

and it's working fine. Now my problem whith the Android app is that my request is received by the server, but the country parameter is always null. My others GET requests are all working perfectly.

Here is my RestClient class :

@Rest(converters = {MappingJacksonHttpMessageConverter.class, FormHttpMessageConverter.class})
    public interface RestClient extends RestClientRootUrl, RestClientSupport{

        @Get("/poke/simple")
        public MessageResponse simplePoke();

        @Get("/api/lanceurs/{name}")
        public LaunchVehicleResponse nameRequest(String name);

        //server doesn't get the parameter here...
        @Post("/api/lanceurs/parPays")
        public LaunchVehicleResponse countryRequest(String country);        
    }

Any help would be appreciated as usual, thanks!

EDIT :

server-side REST api :

@Path("api/lanceurs/parPays")
    @POST
    public String getLanceurByCountry(@FormParam("country") String country)
    {
        initData();
        LaunchVehicleResponse lvr = new LaunchVehicleResponse();
        ArrayList<LaunchVehicle> allv = myDatabase.getDataByCountry(country);
        lvr.setData(allv);
        return parseObjectToJson(lvr);
    }

Solution

  • Ok, it seems I figured out a way to get myself out of this mess.

    I made a class LaunchVehicleRequest on my client, containing (among other things) a country String. When I need to send a request to my server, I instantiate this class and initialize LaunchVehicleRequest.country with the value I want (ex: "USA"). Then I send the whole object to my RestClient.

    LaunchVehicleRequest lvreq = new LaunchVehicleRequest();
    lvreq.setCountry("Europe");
    LaunchVehicleResponse lvr = pm.countryRequest(lvreq);
    

    ...

        @Rest(converters = {MappingJacksonHttpMessageConverter.class, FormHttpMessageConverter.class}, interceptors = { LoggingInterceptor.class } )
        public interface RestClient extends RestClientRootUrl, RestClientSupport, RestClientHeaders{
            @Post("/api/lanceurs/parPays")
            public LaunchVehicleResponse countryRequest(LaunchVehicleRequest request);
    }
    

    I set up the same class on my server-side, which get the request as a string and then convert it in an object.

    @Path("api/lanceurs/parPays")
    @POST
    public String getLanceurByCountry(String request)
    {
        //  request={"country":"USA"}       
        //my json parsing function here
        LaunchVehicleRequest lvreq = parseJsonToRequest(request);
        ...
    }
    

    I don't know is this is the best way, but hey it's working fine now and I'm using my LaunchVehicleRequest class for every different request I can need to, so it's not THAT bad I guess ^^'

    Thanks everyone anyway ;)