Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Android™ Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Android™) curl with Target Outputs

See more CURL Examples
For curl requests that return JSON, you can define output variables that extract specific values directly from the response. Instead of manually parsing the JSON, you provide a JSON path for each value you want. Chilkat uses this path to locate the value and assign it to a variable, which your application can then retrieve using GetVar.

Note: This example requires Chilkat v11.5.0 or greater.

Chilkat Android™ Downloads

Android™ Java Libraries

Android C/C++ Libraries

// 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;

    // This example executes a curl command to retrieve information about a SharePoint site
    // from Microsoft Graph. The request uses variable placeholders that will be replaced
    // at runtime with actual values.
    // 
    // Equivalent curl command:
    // 
    // curl -X GET "https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}" \
    //   -H "Authorization: Bearer ACCESS_TOKEN" \
    //   -H "Accept: application/json"
    // 
    // A typical JSON response looks like this:
    // 
    // {
    //   "@odata.context": "...",
    //   "createdDateTime": "...",
    //   "description": "Test site",
    //   "id": "example.sharepoint.com,...",
    //   "lastModifiedDateTime": "...",
    //   "name": "test",
    //   "webUrl": "...",
    //   "displayName": "test",
    //   "root": {},
    //   "siteCollection": {
    //     "hostname": "example.sharepoint.com"
    //   }
    // }
    // 
    // Rather than processing the entire JSON response, this example extracts only the
    // specific values we care about: id, description, and siteCollection.hostname.
    // These values are located at known JSON paths, so we can define them as "target outputs".

    CkStringBuilder sbTargetCurl = new CkStringBuilder();
    sbTargetCurl.AppendLn("curl -X GET \"https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}\" \\");
    sbTargetCurl.AppendLn("  -H \"Authorization: Bearer ACCESS_TOKEN\" \\");
    sbTargetCurl.AppendLn("  -H \"Accept: application/json\"");

    CkHttpCurl httpCurl = new CkHttpCurl();

    // Configure OAuth2 authentication using the client credentials flow.
    // Secrets (client_id, client_secret, token_endpoint) are retrieved from the
    // local secrets manager because EnableSecrets is set to true.
    CkJsonObject jsonOAuth2 = new CkJsonObject();
    jsonOAuth2.put_EnableSecrets(true);
    jsonOAuth2.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id");
    jsonOAuth2.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret");
    jsonOAuth2.UpdateString("oauth2.scope","https://graph.microsoft.com/.default");
    jsonOAuth2.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint");
    httpCurl.SetAuth(jsonOAuth2);

    // Define values for the variables used in the curl command.
    // These replace the {{sharepoint_hostname}} and {{site_name}} placeholders at runtime.
    httpCurl.SetVar("sharepoint_hostname","example.sharepoint.com");
    httpCurl.SetVar("site_name","test");

    // Define target outputs for the curl command.
    // Each call maps a JSON path in the response to a variable name.
    // After execution, these variables can be retrieved using GetVar.
    httpCurl.AddTargetOutput("id","site_id");
    httpCurl.AddTargetOutput("description","site_description");
    httpCurl.AddTargetOutput("siteCollection.hostname","site_hostname");

    // Execute the curl command. Variable substitution and authentication
    // are handled automatically.
    success = httpCurl.DoYourThing(sbTargetCurl.getAsString());
    if (success == false) {
        Log.i(TAG, httpCurl.lastErrorText());
        return;
        }

    // Load the JSON response from the server.
    CkJsonObject responseJson = new CkJsonObject();
    responseJson.put_EmitCompact(false);
    httpCurl.GetResponseJson(responseJson);

    // Check the HTTP status code returned by the request.
    int statusCode = httpCurl.get_StatusCode();
    Log.i(TAG, "response status code: " + String.valueOf(statusCode));

    if (statusCode != 200) {
        // If the request failed, the JSON response will contain error details
        // instead of the expected data.
        Log.i(TAG, responseJson.emit());
        return;
        }

    // Verify that all target output variables were successfully extracted.
    // Passing "!" to VarDefined returns true only if all target outputs are defined.
    boolean allTargetsDefined = httpCurl.VarDefined("!");
    if (allTargetsDefined == false) {
        Log.i(TAG, httpCurl.lastErrorText());
        Log.i(TAG, "Not all target outputs were located and defined.");
        return;
        }

    // Retrieve and display the extracted values from the response.
    Log.i(TAG, "site_id = " + httpCurl.getVar("site_id"));
    Log.i(TAG, "site_description = " + httpCurl.getVar("site_description"));
    Log.i(TAG, "site_hostname = " + httpCurl.getVar("site_hostname"));

    // Example output:

    // site_id = example.sharepoint.com,9b923c5e-5117-44ad-8b03-cdbb8e19ae85,b2451e19-290f-4f29-9f5d-674c2951a9f7
    // site_description = Test site
    // site_hostname = example.sharepoint.com

  }

  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."
  }
}

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.