Sample code for 30+ languages & platforms
Android™

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.

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;

    CkRest rest = new CkRest();
    boolean bTls = true;
    boolean bAutoReconnect = true;
    success = rest.Connect("example.com",443,bTls,bAutoReconnect);
    if (success == false) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    success = rest.SendReqNoBody("GET","/api/items/123");
    if (success == false) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    int statusCode = rest.ReadResponseHeader();
    if (statusCode < 0) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    //  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
    //  and ResponseHdrValue returns the value at the same index.
    int numHeaders = rest.get_NumResponseHeaders();
    int i;
    for (i = 0; i <= numHeaders - 1; i++) {
        String name = rest.responseHdrName(i);
        if (rest.get_LastMethodSuccess() == false) {
            Log.i(TAG, rest.lastErrorText());
            return;
            }

        String value = rest.responseHdrValue(i);
        if (rest.get_LastMethodSuccess() == false) {
            Log.i(TAG, rest.lastErrorText());
            return;
            }

        Log.i(TAG, name + ": " + value);
        }


  }

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