Android™
Android™
Use an Existing Socket Connection for REST
See more REST Examples
Demonstrates Rest.UseConnection, which supplies an already-connected Socket as the transport for REST requests instead of calling Connect. This is useful when the connection must be established with specific socket options or shared across objects.
Background. Rest can operate over any connected socket. The second argument enables automatic reconnection so a dropped connection is transparently re-established between requests.
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;
// Demonstrates supplying an already-connected socket for the REST transport. This is useful when
// the connection must be established with specific socket options, or reused across objects.
CkSocket socket = new CkSocket();
boolean bSsl = true;
success = socket.Connect("example.com",443,bSsl,5000);
if (success != true) {
Log.i(TAG, socket.lastErrorText());
return;
}
CkRest rest = new CkRest();
// Use the connected socket as the transport. The 2nd argument enables automatic reconnection.
boolean bAutoReconnect = true;
success = rest.UseConnection(socket,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);
}
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."
}
}