how to send cURL in Android to REST service

2019-05-25 07:01发布

I'm newbie in android, I want to get some data from my REST service, but I have some problem to initialize the method what's I send into the my REST service. you know that REST service using cURL to manipulate some data(POST,PUT,GET,DELETE). now how to send POST PUT GET DELETE method via cURL in android. do same as using httppost to send it? or how to send cURL to rest service in android?

2条回答
beautiful°
2楼-- · 2019-05-25 07:18

Using HttpClient you can send POST,PUT,GET,DELETE requests. For an example POST request check here.

查看更多
倾城 Initia
3楼-- · 2019-05-25 07:24
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 // Steven Koelsche` Thanks Brak. Mars you might need to adjust headers also if needed for cURL...

try {
        httppost.setHeader("Content-Type", "application/json");
        httppost.setHeader("Accept", "application/json");
查看更多
登录 后发表回答