如何将Android中的cURL发送到REST服务

时间:2012-12-03 11:56:28

标签: java android rest curl

我是android的新手,我想从我的REST服务中获取一些数据,但是我有一些问题要初始化我发送到我的REST服务的方法。你知道REST服务使用cURL来操作一些数据(POST,PUT,GET,DELETE)。现在如何通过android中的cURL发送POST PUT GET DELETE方法。和使用httppost发送它一样吗?或者如何将cURL发送到android中的休息服务?

2 个答案:

答案 0 :(得分:2)

使用HttpClient,您可以发送POST,PUT,GET,DELETE请求。有关示例POST请求检查here

答案 1 :(得分:2)

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 

// Newventuresmarket.com //史蒂文科尔什 谢谢Brak。 火星你可能还需要调整标题,如果需要cURL ...

try {
        httppost.setHeader("Content-Type", "application/json");
        httppost.setHeader("Accept", "application/json");