-
Notifications
You must be signed in to change notification settings - Fork 5
Quick Start
This quick start applies on version larger or equals to
1.0.0-SNAPSHOT
. For older users lower or equals to0.1.1-BETA
, go to this revision. But, upgrading to1.0.0-SNAPSHOT
is highly recommended, because so major features are added to the API.
A quick start on using the API. It is quite easy to fetch the ETA with a few steps!
Firstly, import necessary imports.
import com.mob41.kmbeta.api.ArrivalManager; //The most important thing to import
import com.mob41.kmbeta.exception.CouldNotLoadDatabaseException; //Optional, Error handling if the database cannot be loaded.
import com.mob41.kmbeta.exception.InvalidArrivalTargetException; //Optional, Error handling if the specified details are wrong, invalid
Secondly, we can specifiy our route and bus stop to fetch ETA data.
//Setup which bus stop you want to fetch arrival time.
final String busno = "2A";
final String stopcode = "LO02T10000";
final String stopname = "LOK WAH BUS TERMINUS";
final int bound = 1;
Thirdly, hook these to the parameters and create a instance of ArrivalManager
.
As the static BusDatabase
instance of ArrivalManager
is still null
. The database will not be loaded twice unless the field of database source is changed, or the bus routes' names' database is ``null```.
By default, without any database sources parameter, the API will try to fetch the database at https://db.kmbeta.ml/kmbeta_db.json. Cellular users warning: The database weights around 5 MB. Open at your risk.. See the code below for more details.
And, will also fetch data (such as, data feed server time and ETA data) automatically after loading the database.
ArrivalManager(String busno, String stop_code, int bound, int language, int loadFromWhere, Object classParent, boolean fromClassResources, boolean showLog) throws InvalidArrivalTargetException, CouldNotLoadDatabaseException
//Hook the variables to the parameters, create a instance, this will show logs, download database from web by default
ArrivalManager arr = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG);
//Alternative: Disables logs
ArrivalManager arr1 = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG, false);
//Alternative: Load from file (Class-path), and show logs
ArrivalManager arr2 = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG, ArrivalManager.DB_LOAD_FROM_FILE, ArrivalManager.class, true, true);
//Alternative: Load from file near the application, and show logs
ArrivalManager arr3 = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG, ArrivalManager.DB_LOAD_FROM_FILE, null, false, true);
If you want to fetch new data after creating the instance, you can do it manually by:
arr.fetchNewData();
Fourthly, we can simply print out the data we fetched. Now we're done!
//Print out the data
//Functions "arr.get*Text()" will automatically formats. See JavaDoc for more details
System.out.println("Arrival time at " + stopname + " is " + arr.getArrivalTimeText());
System.out.println("Remaining arrival time at " + stopname + " is " + arr.getRemainingArrivalTimeText());
System.out.println("Stopname: " + stopname);
System.out.println("Stopcode:" + stopcode);
Here's the full example:
import com.mob41.kmbeta.api.ArrivalManager;
import com.mob41.kmbeta.exception.CouldNotLoadDatabaseException;
import com.mob41.kmbeta.exception.InvalidArrivalTargetException;
public class GetArrival {
public static void main(String[] args) throws InvalidArrivalTargetException, CouldNotLoadDatabaseException{
//Setup which stop you want to fetch arrival time.
final String busno = "2A";
final String stopcode = "LO02T10000";
final String stopname = "LOK WAH BUS TERMINUS";
final int bound = 1;
//Hook the variables to the parameters, create a instance
ArrivalManager arr = new ArrivalManager(busno, stopcode, bound, ArrivalManager.ENGLISH_LANG);
//Tell the manager to fetch new data
arr.fetchNewData(); //This also run getServerTime()
//Print out the data
//Functions "arr.get*Text()" will automatically formats. See JavaDoc for more details
System.out.println("Arrival time at " + stopname + " is " + arr.getArrivalTimeText());
System.out.println("Remaining arrival time at " + stopname + " is " + arr.getRemainingArrivalTimeText());
System.out.println("Stopname: " + stopname);
System.out.println("Stopcode:" + stopcode);
}
}