Sample code for 30+ languages & platforms
Android™

Examine HTTP Response Status Code and Text

See more REST Examples

Demonstrates how to examine the HTTP response status code and text when using the 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();

    // We're going to check https://authenticationtest.com/HTTPAuth/
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("authenticationtest.com",port,bTls,bAutoReconnect);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    // Send a GET request
    String responseText = rest.fullRequestNoBody("GET","/HTTPAuth/");
    if (rest.get_LastMethodSuccess() != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    // The start line of an HTTP response, called the status line, contains the following information:
    // 
    //     The protocol version, usually HTTP/1.1.
    //     A status code, indicating success or failure of the request. Common status codes are 200, 404, or 302
    //     A status text. A brief, purely informational, textual description of the status code to help a human understand the HTTP message.
    // 
    // A typical status line looks like: HTTP/1.1 404 Not Found.

    // The ResponseStatusCode property contains the integer response code:
    Log.i(TAG, "Response status code = " + String.valueOf(rest.get_ResponseStatusCode()));

    // The ResponseStatusText property contains the text (if any) that follows the status code on the status line.
    Log.i(TAG, "Response status text = " + rest.responseStatusText());

    // In this case, the sample output is:
    // 
    // Response status code = 401
    // Response status text = Unauthorized

  }

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