Sample code for 30+ languages & platforms
Android™

curl POST with JSON Input and JSON Output

See more CURL Examples

Demonstrates running a simple curl command with JSON input and JSON output.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Run the following curl command

    //   curl -X POST https://httpbin.org/post \
    //        -H "Content-Type: application/json" \
    //        -d '{
    //              "title": "foo",
    //              "body": "bar",
    //              "userId": 1
    //            }'

    //  The backslashes at the end of lines are not required.  Chilkat ignores them if present.
    CkStringBuilder sbTargetCurl = new CkStringBuilder();
    sbTargetCurl.AppendLn(" curl -X POST https://httpbin.org/post \\");
    sbTargetCurl.AppendLn("      -H \"Content-Type: application/json\" \\");
    sbTargetCurl.AppendLn("      -d '{");
    sbTargetCurl.AppendLn("            \"title\": \"foo\",");
    sbTargetCurl.AppendLn("            \"body\": \"bar\",");
    sbTargetCurl.AppendLn("            \"userId\": 1");
    sbTargetCurl.AppendLn("          }'");

    CkHttpCurl httpCurl = new CkHttpCurl();

    //  Run the curl command.
    success = httpCurl.DoYourThing(sbTargetCurl.getAsString());
    if (success == false) {
        Log.i(TAG, httpCurl.lastErrorText());
        return;
        }

    CkJsonObject responseJson = new CkJsonObject();
    responseJson.put_EmitCompact(false);

    httpCurl.GetResponseJson(responseJson);

    int statusCode = httpCurl.get_StatusCode();
    Log.i(TAG, "response status code: " + String.valueOf(statusCode));

    Log.i(TAG, responseJson.emit());

    //  Output:

    //  response status code: 200
    //  {
    //    "args": {},
    //    "data": "{\r\n            \"title\": \"foo\",\r\n            \"body\": \"bar\",\r\n            \"userId\": 1\r\n          }",
    //    "files": {},
    //    "form": {},
    //    "headers": {
    //      "Content-Length": "96",
    //      "Content-Type": "application/json",
    //      "Host": "httpbin.org",
    //      "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
    //    },
    //    "json": {
    //      "body": "bar",
    //      "title": "foo",
    //      "userId": 1
    //    },
    //    "origin": "123.222.222.222",
    //    "url": "https://httpbin.org/post"
    //  }

    //  ----------------------------------------------------------------------------------
    //  Another example:

    //  curl -X POST https://postman-echo.com/post \
    //       -H "Content-Type: application/json" \
    //       -d '{"foo":"bar"}'

    String targetCurl = "curl -X POST https://postman-echo.com/post -H \"Content-Type: application/json\" -d '{\"foo\":\"bar\"}'";

    //  Run the curl command.
    success = httpCurl.DoYourThing(targetCurl);
    if (success == false) {
        Log.i(TAG, httpCurl.lastErrorText());
        return;
        }

    httpCurl.GetResponseJson(responseJson);

    statusCode = httpCurl.get_StatusCode();
    Log.i(TAG, "response status code: " + String.valueOf(statusCode));

    Log.i(TAG, responseJson.emit());

    //  Output:

    //  response status code: 200
    //  {
    //    "args": {},
    //    "data": {
    //      "foo": "bar"
    //    },
    //    "files": {},
    //    "form": {},
    //    "headers": {
    //      "host": "postman-echo.com",
    //      "content-length": "13",
    //      "content-type": "application/json",
    //      "x-forwarded-proto": "https",
    //      "accept-encoding": "gzip, br"
    //    },
    //    "json": {
    //      "foo": "bar"
    //    },
    //    "url": "https://postman-echo.com/post"
    //  }

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}