因此,我试图使用androidannotation从Android应用程序向我的REST服务器发送一个简单的String。
http://localhost:8080/TestServer_RESTJersey/api/lanceurs/parPays
使用Advanced REST client chrome扩展,我发送参数:
country=Europe
并且工作正常。现在,我遇到的问题是,Android应用程序是服务器收到了我的请求,但是country参数始终为null。我的其他GET请求都运行良好。
这是我的RestClient类:
@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);
}
任何帮助将不胜感激,谢谢!
编辑 :
服务器端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);
}
好的,看来我想出了一种摆脱困境的方法。
我在客户端上创建了一个LaunchVehicleRequest类,其中(其中包括)一个国家/地区字符串。当我需要向服务器发送请求时,我将实例化该类,并使用所需的值(例如:“ USA”)初始化LaunchVehicleRequest.country。然后,将整个对象发送到我的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);
}
我在服务器端设置了相同的类,该类将请求作为字符串获取,然后将其转换为对象。
@Path("api/lanceurs/parPays")
@POST
public String getLanceurByCountry(String request)
{
// request={"country":"USA"}
//my json parsing function here
LaunchVehicleRequest lvreq = parseJsonToRequest(request);
...
}
我不知道这是最好的方法,但是嘿,它现在工作正常,我正在对需要的每个不同请求使用我的LaunchVehicleRequest类,所以我猜这还不错吧^^'
还是谢谢大家;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句