Update examples

This commit is contained in:
Andrea Cavalli 2021-01-16 10:07:08 +01:00
parent 78f0c44025
commit e452d14e7f
2 changed files with 18 additions and 18 deletions

View File

@ -138,8 +138,6 @@ package it.tdlight.example;
import it.tdlight.common.TelegramClient; import it.tdlight.common.TelegramClient;
import it.tdlight.tdlight.ClientManager; import it.tdlight.tdlight.ClientManager;
import it.tdlight.common.Init; import it.tdlight.common.Init;
import it.tdlight.common.Log;
import it.tdlight.common.TDLibException;
import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi;
@ -155,6 +153,9 @@ public class Example {
// Documentation of tdlib methods can be found here: // Documentation of tdlib methods can be found here:
// https://tdlight-team.github.io/tdlight-docs // https://tdlight-team.github.io/tdlight-docs
// An example on how to use tdlight java can be found here:
// https://github.com/tdlight-team/tdlight-java/blob/master/example/src/main/java/it.tdlight.example/Example.java
// A similar example on how to use tdlib can be found here: // A similar example on how to use tdlib can be found here:
// https://github.com/tdlib/td/blob/master/example/java/org/drinkless/tdlib/example/Example.java // https://github.com/tdlib/td/blob/master/example/java/org/drinkless/tdlib/example/Example.java
@ -166,23 +167,13 @@ public class Example {
} }
private static void onUpdateError(Throwable exception) { private static void onUpdateError(Throwable exception) {
if (exception instanceof TDLibException) { System.out.println("Received an error from updates:");
String errorMessage = ((TDLibException) exception).getErrorMessage(); exception.printStackTrace();
System.out.println("Received an error from updates: " + errorMessage);
} else {
System.out.println("Received an error from updates:");
exception.printStackTrace();
}
} }
private static void onError(Throwable exception) { private static void onError(Throwable exception) {
if (exception instanceof TDLibException) { System.out.println("Received an error:");
String errorMessage = ((TDLibException) exception).getErrorMessage(); exception.printStackTrace();
System.out.println("Received an error: " + errorMessage);
} else {
System.out.println("Received an error:");
exception.printStackTrace();
}
} }
} }
``` ```

View File

@ -6,6 +6,7 @@
// //
package it.tdlight.example; package it.tdlight.example;
import it.tdlight.common.ExceptionHandler;
import it.tdlight.common.Init; import it.tdlight.common.Init;
import it.tdlight.common.ResultHandler; import it.tdlight.common.ResultHandler;
import it.tdlight.common.TelegramClient; import it.tdlight.common.TelegramClient;
@ -158,7 +159,7 @@ public final class Example {
case TdApi.AuthorizationStateClosed.CONSTRUCTOR: case TdApi.AuthorizationStateClosed.CONSTRUCTOR:
print("Closed"); print("Closed");
if (!needQuit) { if (!needQuit) {
client = ClientManager.create(new UpdateHandler(), null, null); // recreate client after previous has closed client = ClientManager.create(new UpdateHandler(), new ErrorHandler(), new ErrorHandler()); // recreate client after previous has closed
} else { } else {
canQuit = true; canQuit = true;
} }
@ -305,7 +306,7 @@ public final class Example {
// create client // create client
Init.start(); Init.start();
client = ClientManager.create(new UpdateHandler(), null, null); client = ClientManager.create(new UpdateHandler(), new ErrorHandler(), new ErrorHandler());
client.execute(new TdApi.SetLogVerbosityLevel(0)); client.execute(new TdApi.SetLogVerbosityLevel(0));
// disable TDLib log // disable TDLib log
@ -588,6 +589,14 @@ public final class Example {
} }
} }
private static class ErrorHandler implements ExceptionHandler {
@Override
public void onException(Throwable e) {
e.printStackTrace();
}
}
private static class AuthorizationRequestHandler implements ResultHandler { private static class AuthorizationRequestHandler implements ResultHandler {
@Override @Override
public void onResult(TdApi.Object object) { public void onResult(TdApi.Object object) {