Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Clickatell One API: Read the account balance and currency

See more Clickatell Examples

Request the messaging balance associated with a One API integration key. The response supplies both the decimal amount and its currency. Read the amount as text for display to preserve its precision. A zero balance is a valid result. For example, a billing dashboard can display 1267.92865 ZAR.

Clickatell API reference

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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    //  Supply the key for this One API integration from application configuration.
    http.SetRequestHeader("Authorization","ONE_API_KEY");
    http.SetRequestHeader("Accept","application/json");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpNoBody("GET","https://platform.clickatell.com/v1/balance",resp);
    if (resp.get_StatusCode() == 0) {
        Log.i(TAG, "No HTTP response. Check the connection and retry the lookup.");
        return;
        }

    CkJsonObject reply = new CkJsonObject();
    boolean parsed = reply.Load(resp.bodyStr());
    if ((success != true) or ((resp.get_StatusCode() != 200))) {
        Log.i(TAG, "Clickatell returned HTTP " + String.valueOf(resp.get_StatusCode()));
        if (parsed == true) {
            if (reply.HasMember("error.description") == true) {
                Log.i(TAG, reply.stringOf("error.description"));
                }

            }

        return;
        }

    if (parsed != true) {
        Log.i(TAG, "Expected a JSON response.");
        return;
        }

    //  JsonTypeOf returns 2 for a number and 1 for a string. Zero is a valid amount.
    if ((reply.JsonTypeOf("balance") != 2) or (reply.JsonTypeOf("currency") != 1)) {
        Log.i(TAG, "The balance response is missing the amount or currency.");
        return;
        }

    Log.i(TAG, "Account balance: " + reply.stringOf("balance") + " " + reply.stringOf("currency"));

  }

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