![](/img/trans.png)
[英]Send data using POST to java server from Android client and get JSON response
[英]calling server methods with GET\POST from client in android Java
我正在尋找一種從服務器上的Android客戶端(用Java編寫)調用方法的方法(用Java編寫的Amazon ec2實例)。 我正在尋找類似play框架的東西,我可以在其中編寫一個帶有方法名稱的GET請求(例如,calculateHighScore),並在routes.config中,將get方法設置為從服務器上執行calculateHighScore方法。
我已經閱讀了有關Volley及其通過JSON進行通信的方式,但是我仍然不明白我應該在服務器端編寫什么內容以執行特定方法並返回適當的響應。
您可以嘗試使用Jersey RESTFul Services庫。
例如,在您的服務器上,您可能具有以下代碼:
@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>();
}
}
在客戶端,您可能會像下面這樣的代碼:
[...]
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>>() {});
[...]
這也是一個很好的教程,可以幫助您使用Jersey及其庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.