Android™
Android™
Get Diagnostic JSON from the Last SSH Method Call
See more SSH Examples
Demonstrates the Chilkat Ssh.GetLastJsonData method, which copies structured diagnostic information from the most recently called method into a JsonObject. The only argument is the JsonObject that receives the information.
Background: This gives machine-readable diagnostics as an alternative to parsing
LastErrorText, which is useful for logging or automated error handling. Many methods produce little or no additional JSON, so expect an object that is sometimes nearly empty. Authentication passwords and private-key passphrases are never included, so the output is safe to log.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 the Ssh.GetLastJsonData method, which copies structured diagnostic information
// from the most recently called method into a JsonObject. The only argument is the JsonObject
// that receives the information.
CkSsh ssh = new CkSsh();
int sshPort = 22;
success = ssh.Connect("ssh.example.com",sshPort);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
String password = "mySshPassword";
success = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
String output = ssh.quickCommand("uname -a","utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, output);
// Retrieve diagnostic details about the call that was just made. Many methods produce little
// or no additional JSON. Passwords and private-key passphrases are never included.
CkJsonObject json = new CkJsonObject();
ssh.GetLastJsonData(json);
json.put_EmitCompact(false);
Log.i(TAG, json.emit());
ssh.Disconnect();
}
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."
}
}