簡體   English   中英

在Android Java中使用GET \\ POST從客戶端調用服務器方法

[英]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及其庫。

設計一個API,用於處理來自Android客戶端的GETPOST請求的響應。 Api必須處理來自android客戶端的所有查詢。

例如,如果您希望獲得總體High Score則客戶將發送如下請求:

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

現在,您的api將從您的網址中提取參數,並通過發送具有High Score的響應來響應。

您可以在此處閱讀有關API設計的更多信息。 而更多的在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM