I changed the Volley library to Retrofit. Now I want to access the response body like the Volley library. I searched the internet and came up with this solution, but when I run the program, the program closes and shows a low error. Thank you in advance for your help.
public void getVerifyCode(String mobile, RequestStatus requestStatus) {
Log.i(TAG, "getVerifyCode: Called");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("command", "register_user");
jsonObject.addProperty("mobile", mobile);
Log.i(TAG, "getVerifyCode: requestCode: " + jsonObject.toString());
retrofitApi.getVerifyCode(jsonObject).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i(TAG, "getVerifyCode:onResponse: " + response.toString());
requestStatus.onSuccess(response.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "getVerifyCode:onFailure= " + t.getMessage());
requestStatus.onError(new Exception(t));
}
});
}
@POST(".")
Call<ResponseBody> getVerifyCode(@Body JsonObject body);
I/ApiService: getVerifyCode: Called
I/ApiService: getVerifyCode: requestCode: {"command":"register_user","mobile":"0915*******7"}
I/ApiService: getVerifyCode:onResponse: Response{protocol=http/1.1, code=200, message=OK, url=http://**********ion.freehost.io/}
W/System.err: at ir.*****pp.*****k.ApiService$2.onResponse(ApiService.java:75)
ApiSeviceClass
public void getVerifyCode(String mobile, RequestStatus requestStatus) {
Log.i(TAG, "getVerifyCode: Called");
HashMap<String, String> map = new HashMap<>();
map.put("command", "register_user");
map.put("mobile", mobile);
Log.i(TAG, "getVerifyCode: requestCode: " + map.toString());
retrofitApi.callBack(map).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.isSuccessful()) {
String strResponseBody = response.body().string();
Log.i(TAG, "getVerifyCode:onResponse: " + strResponseBody);
requestStatus.onSuccess(strResponseBody);
}
} catch (IOException e) {
Log.e(TAG, "onResponse: " + e.getMessage());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "getVerifyCode:onFailure= " + t.getMessage());
requestStatus.onError(new Exception(t));
}
});
}
Retrofit Callback
@POST(".")
Call<ResponseBody> callBack(@QueryMap Map<String, String> map);