-
Notifications
You must be signed in to change notification settings - Fork 1
How to
As said in the Readme, using this library is pretty straightforward.
First, import the library to your Android/Java project.
Then, create a HostConfig, which will contain the server's config. If you have your own server or whatever, just modify it:
HostConfig hc = new HostConfig("https://habitrpg.com", "80", "apikey", "userkey");
The API Key and the User key can be found on habitrpg website. Note that as far as I know, you need to be in HTTPS to use it. And the port doesn't matter right now (not needed actually, it could be deleted).
then, you have to create a WebServiceInteraction
, which will hold an interaction with the server. You can either use a PostTask, PostTaskDirection, or GetUser right now (more to come). This WebServiceInteraction
might also require a callback of type OnHabitsAPIResult. You just need to implement one, so that it shows everything when it gets a result.
So, here is a simple callback:
private static OnHabitsAPIResult callback = new OnHabitsAPIResult() {
@Override
public void onUserReceived(User user) {
System.out.println("The player's name is: " + user.getName());
}
@Override
public void onPreResult() {
//What needs to be done before the interaction begin: showing a waiting message...
System.out.println("Contacting the webservice...");
}
@Override
public void onPostResult(double xp, double hp, double gold, double lvl, double delta) {
System.out.println("You won +"+ xp + " experience!");
}
@Override
public void onError(String message) {
System.out.println("Error! " + message);
}
@Override
public void onPostTaskAnswer(HabitItem task) {
System.out.println("Task " + task.getText() + " created!");
}
};
Once you have this callback, you can create your interaction: WebServiceInteraction inter = new GetUser(callback, hc); And then, just call the interaction, and parse the result:
Answer ans = inter.getData();
ans.parse();//parse the object using the callback