Sample code for 30+ languages & platforms
Android™

Http Follow Redirects

See more HTTP Examples

Demonstrates how to automatically follow redirects.

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);

    CkHttp http = new CkHttp();

    //  If you would like to see the HTTP session log to see how 
    //  HTTP redirects were followed.
    http.put_SessionLogFilename("c:/temp/qa_output/sessionLog.txt");

    //  Run the test using this URL.
    //  (Works while httpbin.org keeps the test endpoint available.)

    String url = "https://httpbin.org/redirect-to?url={$redirectUrl}&status_code={$statusCode}";

    http.SetUrlVar("redirectUrl","https://chilkatsoft.com/helloWorld.json");
    http.SetUrlVar("statusCode","302");

    String jsonResponse = http.quickGetStr(url);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, "Response status code: " + String.valueOf(http.get_LastStatus()));
    Log.i(TAG, jsonResponse);

    //  How to tell if the request was redirected, and if so,
    //  what was the final redirect URL?
    Log.i(TAG, "Was Redirected: " + String.valueOf(http.get_WasRedirected()));
    Log.i(TAG, "Final Redirect URL: " + http.finalRedirectUrl());

    //  Output:

    //  Response status code: 200
    //  { "hello": "world" }
    //  Was Redirected: True
    //  Final Redirect URL: https://chilkatsoft.com/helloWorld.json

    //  -----------------------------------------------
    //  Here are the contents of the sessionLog.txt

    //  ---- Sending Sat, 30 Aug 2025 11:41:37 GMT ----
    //  GET /redirect-to?url=https%3A%2F%2Fchilkatsoft.com%2FhelloWorld.json&status_code=302 HTTP/1.1
    //  Host: httpbin.org
    //  Accept: */*
    //  Accept-Encoding: gzip
    //  
    //  
    //  ---- Received Sat, 30 Aug 2025 11:41:37 GMT ----
    //  HTTP/1.1 302 FOUND
    //  Date: Sat, 30 Aug 2025 11:41:38 GMT
    //  Content-Type: text/html; charset=utf-8
    //  Content-Length: 0
    //  Connection: keep-alive
    //  Server: gunicorn/19.9.0
    //  Location: https://chilkatsoft.com/helloWorld.json
    //  Access-Control-Allow-Origin: *
    //  Access-Control-Allow-Credentials: true
    //  
    //  
    //  ---- Sending Sat, 30 Aug 2025 11:41:38 GMT ----
    //  GET /helloWorld.json HTTP/1.1
    //  Host: chilkatsoft.com
    //  Accept: */*
    //  Accept-Encoding: gzip
    //  
    //  
    //  ---- Received Sat, 30 Aug 2025 11:41:38 GMT ----
    //  HTTP/1.1 200 OK
    //  Content-Type: application/json
    //  Last-Modified: Sun, 20 Aug 2023 11:36:27 GMT
    //  Accept-Ranges: bytes
    //  ETag: "34c27f8e5ad3d91:0"
    //  Server: Microsoft-IIS/10.0
    //  X-Powered-By: ASP.NET
    //  Date: Sat, 30 Aug 2025 11:41:38 GMT
    //  Content-Length: 22
    //  
    //  { "hello": "world" }

  }

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