我可以发送带有1个字符串参数的Android HttpGet(不是HttpPost)吗?

时间:2013-01-05 07:01:37

标签: android rest parameters http-post http-get

我正在编写一个RESTful API& Android客户端同时我和我正在努力从服务器中提取用户配置文件。我觉得这绝对应该是一个获取请求只是我只提取现有数据并且我不在我的数据库中添加/编辑任何东西,但我确实需要一个user_id参数来查询相应的配置文件。我可以发送一个小的变量和我的HttpGet一些我在这种情况下应该如何使用HttpPost吗?

2 个答案:

答案 0 :(得分:0)

GET可以支持添加变量/参数。例如,您可以创建一个如下所示的Url

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

答案 1 :(得分:0)

Android使用Apache's HTTPClient。所以,复制他们的tutorial code

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;
}