Reputation: 73
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)
Upvotes: 0
Views: 2247
Reputation: 73
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);
Upvotes: 1
Reputation: 160
enqueue callback method onResponse's response.body()
is ResponseBody.
In my opinion, I recommend using Gson or Moshi to add it to Retrofit. Then you don't always have to translate using JsonObject like that.
In addition, if you add suspend
to the head of the function of the api service interface you created, you can return a Response
or the model
itself without enqueue in the coroutine. (Retrofit automatically handles it in a background thread, so Coroutine does not need to be handled in a scope other than the UI.)
Also, Response object supports ErrorBody, ResultCode, etc.
Upvotes: 2