我是安卓的新手,我想从我的 REST 服务中获取一些数据,但我在初始化发送到我的 REST 服务的方法时遇到了一些问题。 你知道REST服务使用cURL来操作一些数据(POST,PUT,GET,DELETE)。 现在如何在安卓中通过cURL发送POST PUT GET DELETE 方法。 做与使用HTTPPOST发送它相同的操作吗? 或者如何将 cURL 发送到 REST 服务在安卓中?
使用 HttpClient 可以发送 POST、PUT、GET、DELETE 请求。有关示例 POST 请求,请查看此处。
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史蒂文·科尔舍'谢谢布拉克。火星 如果需要 cURL,您可能还需要调整标头...
try {
httppost.setHeader("Content-Type", "application/json");
httppost.setHeader("Accept", "application/json");