简体   繁体   English

如何使用 Java 中的 Retrofit 解析嵌套的多个 Json 对象

[英]How to Parse Nested - Multiple Json Objects using Retrofit in Java

I'm having a problem with Retrofit.我遇到了 Retrofit 的问题。

When I make a call to my REST API, every time the response.body() is not successfull and null当我打电话给我的 REST API 时,每次response.body()都不成功并且 null

I tried to make the same call with Postman and the body contains the correct answer.我尝试使用 Postman 进行相同的调用,并且正文包含正确的答案。

The JSON response Body: JSON 响应体:

{
    "error": false,
    "message": "API-Request successful",
    "users": {
        "0": {
            "userID": 11,
            "firstName": "David",
            "userName": "david",
            "birthDate": "2020-10-19",
            "gender": 1,
            "country": "Linz",
            "aboutmeText": "Testeintrag 3",
            "status": "I like Lookly."
        },
        "1": {
            "userID": 43,
            "firstName": "Sarah",
            "userName": "sarahh",
            "birthDate": "2001-12-14",
            "gender": 0,
            "country": "Austria",
            "aboutmeText": "Lookly ist eine hervorragende App!",
            "status": "Hey there! I am using Lookly."
        }
    }
}

My request: response.body() is not successfull and null我的请求: response.body()不成功和 null

public void updateListView(String latitude, String longitude) {
        Call<ResponseBody> call = RetrofitClient
                .getInstance()
                .getApi()
                .standard(
                        latitude,
                        longitude
                );

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccessful()) {
                    //the response-body is already parseable to your ResponseBody object
                    ResponseBody responseBody = (ResponseBody) response.body();
                    //you can do whatever with the response body now...
                    String responseBodyString= null;
                    try {
                        responseBodyString = responseBody.string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Log.d("Response body", responseBodyString);
                }
                else  {
                    Log.d("Response errorBody", String.valueOf(response.errorBody()));
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("onFalure", t.getMessage());
                Toast.makeText(TestRequest.this, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

Successfull Postman Response:成功 Postman 响应:

在此处输入图像描述

you can generate equivalent java code of request in postman itself您可以在 postman 本身中生成等效的 java 请求代码

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM