Event Callbacks in Java
Event callbacks in Java (including Android) are supported starting in v9.5.0.52. To receive event callbacks, first create a Java class derived from one of the Chilkat event callback classes. The event callback classes are: CkBaseProgress, CkHttpProgress, CkZipProgress, CkFtp2Progress, CkMailManProgress, CkTarProgress, and CkSFtpProgress.
All future Chilkat classes will only use CkBaseProgress. Theses event callback classes will be documented in the “Events” section of each class’s online reference documentation.
Here is an example of an HTTP download using event callbacks in Java. First we have the MyHttpProgress derived from CkHttpProgress. Following that, we have the simple Java program that installs the event handler and does the HTTP download. Event callbacks will also happen with the asynchronous version of the methods, but beware of the fact that when asynchronous, the task is running in a background thread and the callback occurs in the background thread.
import com.chilkatsoft.CkHttpProgress; import com.chilkatsoft.CkTask; public class MyHttpProgress extends CkHttpProgress { public boolean AbortCheck() { System.out.println("AbortCheck"); // Return true to abort, false to allow the method to continue. return false; } // pctDone is a value from 0 to 100 // (it is actually value from 0 to the PercentDoneScale property setting) public boolean PercentDone(int pctDone) { System.out.println(pctDone); // Return true to abort, false to allow the method to continue. // Note: A PercentDone event is the equivalent of an AbortCheck. // When PercentDone events are frequently firing, AbortCheck events are suppressed. // AbortCheck events will fire when the time between PercentDone events is longer // than the HeartbeatMs property setting. return false; } public void ProgressInfo(String name, String value) { System.out.println(name + ": " + value); } public void TaskCompleted(CkTask task) { System.out.println("task completed!"); } }
The synchronous Java program w/ event callbacks… (the asynchronous version follows..)
import com.chilkatsoft.CkHttp; public class HttpWithEvents { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { CkHttp http = new CkHttp(); // Install an event callback handler to get progress events. MyHttpProgress myProgress = new MyHttpProgress(); http.put_EventCallbackObject(myProgress); boolean success; // Any string unlocks the component for the 1st 30-days. success = http.UnlockComponent("Anything for 30-day trial"); if (success != true) { System.out.println(http.lastErrorText()); return; } // Download a file at a URL. success = http.Download("http://www.chilkatsoft.com/download/9.5.0.51/ChilkatDotNet45-9.5.0-x64.zip","ChilkatDotNet45-9.5.0-x64.zip"); if (success != true) { System.out.println(http.lastErrorText()); return; } System.out.println("OK"); } }
The asynchronous Java program w/ event callbacks…
import com.chilkatsoft.CkHttp; import com.chilkatsoft.CkTask; public class AsyncHttp { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { CkHttp http = new CkHttp(); // Install an event callback handler to get progress events. MyHttpProgress myProgress = new MyHttpProgress(); http.put_EventCallbackObject(myProgress); boolean success; // Any string unlocks the component for the 1st 30-days. success = http.UnlockComponent("Anything for 30-day trial"); if (success != true) { System.out.println(http.lastErrorText()); return; } // Download a file at a URL. CkTask task = http.DownloadAsync("http://www.chilkatsoft.com/download/9.5.0.51/ChilkatDotNet45-9.5.0-x64.zip","ChilkatDotNet45-9.5.0-x64.zip"); if (success != true) { System.out.println(http.lastErrorText()); return; } task.put_UserData("chilkatDotNet45"); if (!task.Run()) { System.out.println(task.lastErrorText()); return; } System.out.println("OK, task is running..."); // Wait a max of 10 seconds for it to finish. success = task.Wait(10000); // What is the task status? System.out.println("task status = " + task.status()); } }