0

我同时编写了一个 RESTful API 和 Android 客户端,我目前正在从服务器中提取用户配置文件。我觉得这绝对应该是一个获取请求,因为我只提取现有数据而不向我的数据库添加/编辑任何内容,但我确实需要一个 user_id 参数才能查询适当的配置文件。我可以只发送一个很小的变量以及我的 HttpGet 一些在这种情况下我应该如何或应该使用 HttpPost 吗?

4

2 回答 2

0

GET可以支持添加变量/参数。例如,您可以制作如下所示的Url

http://yourwebsite.com/script.php?user_id=19898424

于 2013-01-05T07:07:15.787 回答
0

Android 使用Apache 的 HTTPClient。所以,复制他们的教程代码

public void sendStringTo(String remoteUrl, String myString) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(remoteUrl+"?string1="+myString);
    HttpResponse response1 = httpclient.execute(httpGet);

    // The underlying HTTP connection is still held by the response object 
    // to allow the response content to be streamed directly from the network socket. 
    // In order to ensure correct deallocation of system resources 
    // the user MUST either fully consume the response content  or abort request 
    // execution by calling HttpGet#releaseConnection().

    try {
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity1);
    } finally {
        httpGet.releaseConnection();
    }
    return;
}
于 2013-01-05T07:07:55.620 回答