How to execute RESTful POST requests with Android

I am trying to run a REST Post for the first time, and I don’t quite know where to start.

I am interacting with the WordPress REST API and I am trying to use this endpoint: /sites/$site/posts/$post_ID/replies/newwhich is used to send a new comment to a specific post.

I think that I am well versed in working with GET requests since I have successfully dealt with several of them. With that, I could say everything I needed to tell the server regarding the URL, but there seems to be one more step with POST requests. And my question is: what are these steps?

Do I complete the content that I want to send to JSONObject and publish it? If so, how do I place it? Do I need to somehow create a statement, similar to how I will build a statement to execute in a database? Or is it really possible to pass my content through a URL as request parameters?

I know that this question is a bit on the side open to SO, but I could not find a good tutorial that answers these questions. If you know one, suggest it.

(I do it all in an Android app)

+5
source share
2 answers

My answer is taken directly from another SO answer seen here Sending POST data to Android , but I cut and skipped the answer here for convening, hope this helps

Http Apache Commons - . android. , HTTP-, .

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", "Hi"));
        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
    }
} 
+7

script , POST script , script .

:

Java HTTP POST ~~~ > PHP ~~~~ > MySql.

PHP PHPAcademy youtube.

http://www.youtube.com/course?list=EC442FA2C127377F07

PHP JSON .

0

All Articles