-
Notifications
You must be signed in to change notification settings - Fork 12
Examples: Prerequisites
In this page, we will introduce some common parts used by all examples, including:
- KKAPIBase (ExampleAPI.java)
- KKAPIRequest
- KKAPIListener
- ExampleAPP.java
- KKService (ExampleService.java)
- KKActivity
- KKActionBar
When using API wrapper of KAT, it is recommended that you extend KKAPIBase and create an API base of your own application.
When creating your own APIBase, it is required that you override parse() function by yourself. The parse() function is called when the API has done fetching data, which is after execute() is called.
Here is an example of parse():
@Override
protected int parse(String data) {
try {
/* Do your parsing here */
} catch (JSONException e) {
return ErrorCode.INVALID_API_FORMAT;
}
return ErrorCode.NO_ERROR;
}
Note that the parse() function decides where the api goes next.
When ErrorCode.NO_ERROR is returned, the API will then go to onAPIComplete(); else, goes to onAPIError().
These two functions are in KKAPIListener, and should be implemented by you.
KKAPIBase.ErrorCode is a public class that defines return codes as follows:
public static class ErrorCode {
public static final int NO_ERROR = 0;
public static final int NETWORK_NOT_AVAILABLE = -101;
public static final int UNKNOWN_SERVER_ERROR = -102;
public static final int INVALID_API_FORMAT = -103;
}
You may extend this class and add your own definitions. It is recommended that you define error/fail situations as negative value.
KKAPIRequest is used to build a HTTP API request for KKAPIBase to execute. You may new() a KKAPIRequest and add GET or POST parameters to it, like the following example:
KKAPIRequest request = new KKAPIRequest(APIUrl, null);
request.addGetParam("Id", URLEncoder.encode(Id, "UTF-8"));
request.addPostParam("testString", testString);
You may also add mutilpart POST by calling addMultiPartPostParam().
After the request is built, you may then call execute() to put API to work. The following code fragment is an example of how it works:
public void start() {
try {
KKAPIRequest request = new KKAPIRequest(APIUrl, null);
/** Build your request here */
execute(request);
} catch (Exception e) {
e.printStackTrace();
}
}
KKAPIListener catches the event triggered by KKAPIBase, it is an abstract class which has the following functions for you to implement:
public void onAPIComplete() {};
public void onAPIError(int errorCode) {};
where onAPIComplete() is called when API result is parsed with ErrorCode.NO_ERROR returned, and onAPIError() otherwise.
Remember to set your API listener to the API for it to work properly:
exampleAPI = new ExampleAPI();
exampleAPI.setAPIListener(exampleAPIListener);
This is optional since it does not include any KAT-specific contents.
However, if you want to turn KKDebug functions on, be sure to add
KKDebug.setDebugEnabled(true);
in your application extend.
If you need to start service, add startService() here as well.
When using KKService, it is also recommended that you implement your own BaseService by extending KKService and the following function MUST be implemented:
@Override
public void initServiceComponent() {
}
Since KKService.initServiceComponent() will be called before KKServiceActivity.onServiceStarted(), you should put things that needs to be initialized first before an Activity uses it in this function to avoid application crash.
KKActivity is an extend of Activity class, with ActionBar support down to Android 2.x.
Note that getActionBar() should be changed to getKKActionBar() to work correctly.
If you intent to act with service in your activity, use KKServiceActivity instead of KKActivity.
KKActionBar is a self-made ActionBar for compatibility with Android 2.x.
You may call setTitle() to set the main title of the action bar, or call setSubtitle() to set the subtitle.
You may also design custom style. It is recommended that you put your custom style in values/styles.xml along with other styles. Below is an example of custom style:
<resources>
<style name="ExampleActivityTheme" parent="KKActivityTheme">
<item name="KKActionBarStyle">@style/ActionBar</item>
</style>
<style name="ActionBar" parent="@style/KKActionBarStyle">
<item name="android:icon">@drawable/icon_logo</item>
<item name="android:background">@drawable/background_actionbar</item>
</style>
<style name="ActionBar.Title" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/white</item>
</style>
<style name="ActionBar.Subtitle" parent="@android:style/TextAppearance.Small">
<item name="android:textSize">12sp</item>
<item name="android:textColor">@color/black</item>
</style>
</resources>