Sample code for 30+ languages & platforms
Android™

Example: Http.CloseAllConnections method

See more HTTP Examples

Demonstrates how to call the CloseAllConnections method.

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;

    CkHttp http = new CkHttp();

    //  Demonstrate 
    String url = "https://finnhub.io/api/v1/quote?symbol={$symbol}&token={$api_key}";

    //  When the request is sent, the {$symbol} is replaced with "MSFT"
    //  and the {$api_key} is replaced with "1234567890ABCDEF"
    http.SetUrlVar("symbol","MSFT");
    http.SetUrlVar("api_key","1234567890ABCDEF");

    CkStringBuilder sbJson = new CkStringBuilder();
    success = http.QuickGetSb(url,sbJson);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    int statusCode = http.get_LastStatus();
    Log.i(TAG, http.lastResponseHeader());

    //  The response header contains this:

    //  Date: Tue, 19 Aug 2025 12:18:56 GMT
    //  Content-Type: application/json; charset=utf-8
    //  Transfer-Encoding: chunked
    //  Connection: keep-alive
    //  Content-Encoding: gzip

    //  The "Connection: keep-alive" header ensures the server keeps the connection open for subsequent requests.
    //  If the server responds with a "Connection: close" header, it indicates the connection will be closed after the response. 
    //  Consequently, Chilkat will also close the connection, requiring a new one to be established for any subsequent requests to the same server.

    //  If your application uses the same HTTP object instance to send requests to a different server, a new connection will be established with that server.
    //  Existing connections remain open, with Chilkat maintaining up to 10 open connections. 
    //  If all 10 connections are in use, Chilkat will close the least recently used connection to connect to a new server.

    //  Your application can explicitly close all open connections like this:
    success = http.CloseAllConnections();
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, "Success.");

  }

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