如何在改造中访问响应体?

how to Access to the Response body in the retrofit?

我将 Volley 库更改为 Retrofit。现在我想像 Volley 库一样访问响应主体。我在互联网上搜索并提出了这个解决方案,但是当我 运行 程序时,程序关闭并显示低错误。预先感谢您的帮助。


api服务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));
        }
    });
}

改装回调

@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.onResponse(ApiService.java:75)

入队回调方法onResponse的response.body()是ResponseBody

在我看来,我更推荐使用Gson或Moshi将其添加到Retrofit中。 然后你不必总是像那样使用 JsonObject 进行翻译。

此外,如果您将suspend添加到您创建的api服务接口的函数头部,您可以return一个Responsemodel 本身没有在 协程 中排队。 (Retrofit 会自动在后台线程中处理它,因此 Coroutine 不需要在 UI 以外的范围内处理。) 此外,Response对象支持ErrorBody、ResultCode等

最后,经过这些改动,我得出了一个结论。

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

改装回调

 @POST(".")
    Call<ResponseBody> callBack(@QueryMap Map<String, String> map);