Reza
Reza

Reputation: 73

how to Access to the Response body in the retrofit?

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.


apiService Class

   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));
        }
    });
}

Retrofit Callback

@POST(".")
Call<ResponseBody> getVerifyCode(@Body JsonObject body);

Logcat

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

Answers (2)

Reza
Reza

Reputation: 73

Finally, with these changes, I was able to reach a conclusion.

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

PJH
PJH

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

Related Questions