Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android Java中使用GET\POST从客户端调用服务器方法_Java_Android_Client Server_Android Volley - Fatal编程技术网

在android Java中使用GET\POST从客户端调用服务器方法

在android Java中使用GET\POST从客户端调用服务器方法,java,android,client-server,android-volley,Java,Android,Client Server,Android Volley,我正在寻找一种从服务器(用Java编写的AmazonEC2实例)中的android客户端(用Java编写)调用方法的方法。我正在寻找类似play framework的东西,在这里我可以编写一个带有方法名(比如calculateHighScore)的GET请求,并在routes.config中设置GET方法以从服务器执行calculateHighScore方法 我已经读过关于volley及其JSON通信方式的文章,但我仍然不明白我应该在服务器端写些什么来执行特定的方法并返回适当的响应。设计一个AP

我正在寻找一种从服务器(用Java编写的AmazonEC2实例)中的android客户端(用Java编写)调用方法的方法。我正在寻找类似play framework的东西,在这里我可以编写一个带有方法名(比如calculateHighScore)的GET请求,并在routes.config中设置GET方法以从服务器执行calculateHighScore方法


我已经读过关于volley及其JSON通信方式的文章,但我仍然不明白我应该在服务器端写些什么来执行特定的方法并返回适当的响应。

设计一个API来处理来自android客户端的
GET
POST
请求的响应。Api必须处理来自android客户端的所有查询

例如,如果您想要您的总体
高分
,客户端将发送如下请求:

test.com/api/?m=“calculateHighScore”

现在,您的api将从url中提取参数,并通过发送带有
High Score
的响应进行响应

你可以在这里了解更多。等等。

您可以尝试使用RESTFul服务库

例如,在您的服务器上,您可以有如下代码:

@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>>() {});
[...]
@Path(“/your_class”)
公共课你的课{
[...]
@职位
@路径(“/your\u方法”)
@使用(MediaType.TEXT\u PLAIN)
@产生(MediaType.APPLICATION_JSON)
公共ArrayList方法(字符串输入){
[...]
返回新的ArrayList();
}
}
在客户端,您可以使用如下代码:

@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>>() {});
[...]
[…]
setIteratorProvider(新的AndroidServiceInteratorProvider());
ClientConfig ClientConfig=newdefaultclientconfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE\u POJO\u映射,Boolean.TRUE);
clientConfig.getClasses().add(JacksonJsonProvider.class);
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,“您的输入”);

ArrayList=response.getEntity(新的GenericType是一个很好的教程,可以帮助您使用Jersey及其库。

为android客户端的
GET
POST
请求的特定响应设计一个API。示例:
test.com/API/?method=“calculateHighScore”
提取api中的参数并做出适当的响应。是的!这就是我要找的!我不知道如何设计api,你知道一个好的指南/教程吗?