Tooth
It is the most simplified java connection to server using http url connection This utility class for the moment accepts the following:
- Methods GET and POST, and GET being a default method;
- Parameter data as HashMap<String, String>
It runs on a separate thread and has a connection listener that listens for a response or an error.
It does not depend on a third party library.
OnConnectionListener
It is located in org.smirl.tooth.listener package, it is an interface that enables listening to connection when result is returned or exception is raised from the server.
public interface OnConnectionListener {
void onResponse(String response);
void onError(int errorCode, String error);
}
Method
It is an enum inside Tooth class. It supports so far GET and POST
public enum Method {
GET("GET"),
POST("POST");
/**
* the String value of the Method
*/
public String value;
Method(String method) {
this.value = method;
}
}
IMPLEMENTATION
This is a simple example of how Tooth class can be implemented:
public void test(){
String url = "http://api.smirl.org/tabwayane/test.php";
HashMap<String, String> params = new HashMap<>();
params.put("y", "sikia bintu");
Tooth tooth = new Tooth(Tooth.Method.POST, url, params, new OnConnectionListener(){
@Override
public void onResponse(String response) {
System.out.println("RESPONSE : " + response);
}
@Override
public void onError(int errorCode, String error) {
System.out.println("ERROR : Code=" + errorCode + ", Error=" + error);
}
});
}
The purpose is to create a simple and boiled down utility.
It has four constructors that enables configuration:
- this is a full set constructor
public Tooth(Method method, String url, HashMap<String, String> data, OnConnectionListener listener)
- here there is no data to be sent
public Tooth(Method method, String url, OnConnectionListener listener)
- here we apply the default GET method
public Tooth(String url, HashMap<String, String> data, OnConnectionListener listener)
- here we apply the default GET method and there is no data to be sent too.
public Tooth(String url, OnConnectionListener listener)