Android™
Android™
Disconnect from a REST Server
See more REST Examples
Demonstrates Rest.Disconnect, which closes the current HTTP connection. The argument is the maximum number of milliseconds to wait for the close to complete.
Background. Connections are normally kept alive for reuse. Disconnect is used when an application wants to explicitly release the connection rather than relying on keep-alive or object destruction.
Chilkat Android™ Downloads
// 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();
// Connect to the REST server. The 3rd argument enables TLS (use true for HTTPS on port 443),
// and the 4th enables automatic reconnection if the connection is dropped between requests.
boolean bTls = true;
boolean bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
String responseText = rest.fullRequestNoBody("GET","/api/status");
if (rest.get_LastMethodSuccess() == false) {
Log.i(TAG, rest.lastErrorText());
return;
}
Log.i(TAG, responseText);
// Close the connection. The argument is the maximum number of milliseconds to wait for the
// close to complete.
boolean bDisconnected = rest.Disconnect(200);
if (bDisconnected != true) {
Log.i(TAG, "The disconnect did not complete within the wait time.");
}
Log.i(TAG, "Done.");
}
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."
}
}