Sample code for 30+ languages & platforms
Android™

POST application/x-www-form-urlencoded using REST API

See more REST Examples

Demonstrates how to send a POST with query params (x-www-form-urlencoded) using the Chilkat REST object.

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;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkRest rest = new CkRest();

    // This example will send to https://www.chilkatsoft.com/echoPost.asp

    // Make the initial connection (without sending a request yet).
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("www.chilkatsoft.com",port,bTls,bAutoReconnect);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    // Provide query params.
    rest.AddQueryParam("firstName","John");
    rest.AddQueryParam("lastName","Doe");
    rest.AddQueryParam("company","Bisco Bits Ltd.");

    String responseStr = rest.fullRequestFormUrlEncoded("POST","/echoPost.asp");
    if (rest.get_LastMethodSuccess() != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    // When successful, the response status code will equal 200.
    if (rest.get_ResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        Log.i(TAG, "response status code = " + String.valueOf(rest.get_ResponseStatusCode()));
        Log.i(TAG, "response status text = " + rest.responseStatusText());
        Log.i(TAG, "response header: " + rest.responseHeader());
        Log.i(TAG, "response body (if any): " + responseStr);
        Log.i(TAG, "---");
        Log.i(TAG, "LastRequestStartLine: " + rest.lastRequestStartLine());
        Log.i(TAG, "LastRequestHeader: " + rest.lastRequestHeader());
        return;
        }

    Log.i(TAG, responseStr);
    Log.i(TAG, "Success.");

  }

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