Sample code for 30+ languages & platforms
Android™

URL-Encode a MIME Body

See more MIME Examples

Demonstrates the Chilkat Mime.UrlEncodeBody method, which replaces the current body with an application/x-www-form-urlencoded-style representation using the named charset. The only argument is the charset; it is a void method.

Background: This transforms the body into the form-encoding used by HTML form submissions — spaces to +, reserved characters to percent-escapes — interpreting the text through the given charset first so non-ASCII characters encode correctly. It is a niche operation for constructing form-style payloads within a MIME part.

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;

    //  Demonstrates the Mime.UrlEncodeBody method, which replaces the current body with an
    //  application/x-www-form-urlencoded representation using the named charset.  The only argument is
    //  the charset.  It is a void method.

    CkMime mime = new CkMime();

    success = mime.SetBodyFromPlainText("name=John Doe & city=New York");
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    //  URL-encode the body: spaces become "+", reserved characters become %-escapes.
    mime.UrlEncodeBody("utf-8");

    String encodedBody = mime.getBodyDecoded();
    if (mime.get_LastMethodSuccess() == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, encodedBody);

  }

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