【问题标题】:Save JSON object from response body. Retrofit2从响应正文中保存 JSON 对象。改造2
【发布时间】:2020-04-23 15:50:14
【问题描述】:

这是我收到的 response.body

D/OkHttp: <-- 201 Created http://192.168.0.2:8000/api/surveys (962ms)
D/OkHttp: Host: 192.168.0.2:8000
D/OkHttp: Date: Thu, 23 Apr 2020 15:25:40 GMT
D/OkHttp: Connection: close
D/OkHttp: X-Powered-By: PHP/7.3.10
D/OkHttp: Cache-Control: no-cache, private
D/OkHttp: Date: Thu, 23 Apr 2020 15:25:40 GMT
D/OkHttp: Content-Type: application/json
D/OkHttp: X-RateLimit-Limit: 60
D/OkHttp: X-RateLimit-Remaining: 59

//this is what I want to save
D/OkHttp: [{"id":2,"evaluation_id":1,"user_id":null,"group_id":1,"created_at":"2020-04-20 11:04:31",
"updated_at":"2020-04-20 11:04:31","answered":false,"evaluation":{"id":1,"survey_id":1,
"init_date":"2020-04-13 12:00:00","end_date":"2020-04-30 12:00:00","report":0,"report_date":null,
"report_end":null,"created_at":"2020-04-20 11:04:21","updated_at":"2020-04-20 11:04:21",
"survey":{"id":1,"name":"asdfasdfds group","description":"<p>adfadsfa<strong><em>dsf<\/em>
<\/strong>asdfadsf asd asdf f sad fsd<\/p>","user_id":1,"anonymous":0,"created_at":"2020-04-20 11:04:21",
"updated_at":"2020-04-20 11:04:31"}}},{"id":5,"evaluation_id":2,"user_id":1,"group_id":null,
"created_at":"2020-04-21 11:00:19","updated_at":"2020-04-21 11:00:19","answered":true,
"evaluation":{"id":2,"survey_id":2,"init_date":"2020-04-13 12:00:00","end_date":"2020-04-24 12:00:00","report":0,
"report_date":null,"report_end":null,"created_at":"2020-04-20 11:05:43","updated_at":"2020-04-20 11:05:43",
"survey":{"id":2,"name":"asdfa single","description":"<p>asdfa<\/p>","user_id":1,"anonymous":1,
"created_at":"2020-04-20 11:05:43","updated_at":"2020-04-21 11:00:19"}}}]

D/OkHttp: <-- END HTTP (1123-byte body)

我已经为主对象内的对象创建了 POJO 模型,这是一个改造响应,所以我现在就这样

@Override
public void onResponse(Call<Survey> call, Response<Survey> response) {
    if (!response.isSuccessful()) {
        FancyToast.makeText(activity, getString(R.string.error),
                FancyToast.LENGTH_SHORT, FancyToast.CONFUSING,
                R.drawable.error_outline, false).show();
        return;
    }

    System.out.println("response: " + response.body());
}

如何从响应正文中获取 JSON 然后保存在模型中?

【问题讨论】:

    标签: android retrofit2


    【解决方案1】:

    你的模型类应该是这样的:

    class Model : ArrayList<ModelItem>(){
        data class ModelItem(
            val answered: Boolean = false,
            val created_at: String = "",
            val evaluation: Evaluation = Evaluation(),
            val evaluation_id: Int = 0,
            val group_id: Int = 0,
            val id: Int = 0,
            val updated_at: String = "",
            val user_id: Int = 0
        ) {
            data class Evaluation(
                val created_at: String = "",
                val end_date: String = "",
                val id: Int = 0,
                val init_date: String = "",
                val report: Int = 0,
                val report_date: Any = Any(),
                val report_end: Any = Any(),
                val survey: Survey = Survey(),
                val survey_id: Int = 0,
                val updated_at: String = ""
            ) {
                data class Survey(
                    val anonymous: Int = 0,
                    val created_at: String = "",
                    val description: String = "",
                    val id: Int = 0,
                    val name: String = "",
                    val updated_at: String = "",
                    val user_id: Int = 0
                )
            }
        }
    }
    

    确保您在改造中使用 GSON 转换器库。

    【讨论】:

    • 我有 3 个独立的 POJO 类,一个用于主要对象,一个用于评估,一个用于调查?这就是你的意思我应该只有一个文件吗?
    • 无论您将类放入多少文件,重要的是您根据响应层次结构创建对象
    • 对,我的文件是分开的,但它们确实遵循您上面显示的结构,我确实删除了一些不必要的字段,例如 created_at、update_at 和报告日期。之后如何将请求中的数据保存到这些数据中?
    • 只说Model model = response.body();
    • 即使响应中有多个模型?
    猜你喜欢
    • 2017-04-19
    • 2017-08-07
    • 2021-12-09
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多