A light way to make communication between android and server using a predefine structure server response
- Prepare your request
- Use SpeedController to make request asynctask without building external or internal asynctask class
- Handle your server response very easy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.yann-yvan:CodeHttp:V1.2'
}
<manifest ...>
<application...>
<meta-data
android:name="BASE_URL"
android:value="http://baseurl/" />
</application>
</manifest>
<manifest ...>
<application...>
<meta-data
android:name="DEBUG_MODE"
android:value="true" />
</application>
</manifest>
final DefaultResponse response = new DefaultResponse("login", false);
//First argument the route of the request
//Second argument is a boolean that define whether the request should include token or not
PrepareRequest request = new PrepareRequest();
request.setOutgoing("json data");
//or you can set a JSonObject directly like this
request.getOutgoingJsonObject().put("email", "foo@codehttp.com");
response.setPrepareRequest(request);
//for a POST request use this method
TestController.this.post(response);
//for a GET request use this method
TestController.this.get(response);
//a complete sample will be display below
Now with this controller you can execute request in a very simple way
SpeedController.run(
PrepareRequest.Method.POST,
new DefaultResponse("test", false), new SpeedController.OnAfterExecute() {
@Override
public void play(DefaultResponse response) {
Toast.makeText(MainActivity.this, "Good no error", Toast.LENGTH_SHORT).show();
}
@Override
public void foundException(Exception e) {
if (e instanceof NoInternetException){
Toast.makeText(MainActivity.this, "OOPS "+e.getMessage(), Toast.LENGTH_SHORT).show();
}else if (e instanceof RequestException){
Toast.makeText(MainActivity.this, "OOPS "+e.getMessage(), Toast.LENGTH_SHORT).show();
}else{//JSonException
Toast.makeText(MainActivity.this, "OOPS "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
These fields should always be present in your server response
{
"status":true,
"message":1000
}
Your json file should not have more than 4 fields and not let's than 2 fields
{
"status":true,
"message":1000,
"token":"your auth token",
"data":"what ever you want"
}
Here you can identify how to make and action according to server message
switch (RequestCode.requestMessage(response.getMessage())) {
case SUCCESS:
System.out.print(response.getPrepareRequest().getIncoming());
break;
case FAILURE:
break;
case MISSING_DATA:
break;
case EXPIRED:
break;
case UNKNOWN_CODE:
break;
}
Complete TestController Sample code Sample controller
Please check to see supported message Supported message
Auth | DefResponse | Token |
---|---|---|
ACCOUNT_NOT_VERIFY(1100), | SUCCESS(1000), | TOKEN_EXPIRED(1), |
WRONG_USERNAME(1101), | FAILURE(1001), | BLACK_LISTED_TOKEN(2), |
WRONG_PASSWORD(1102), | MISSING_DATA(1002), | INVALID_TOKEN(3), |
WRONG_CREDENTIALS(1103), | EXPIRED(1003), | NO_TOKEN(4), |
ACCOUNT_VERIFIED(1104), | UNKNOWN_CODE(0); | USER_NOT_FOUND(5), |
UNKNOWN_CODE(0); | UNKNOWN_CODE(0); |