从响应体中保存JSON对象。Retrofit2

问题描述 投票:0回答:1

这是我收到的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个回答
0
投票

这里是你的模型类应该是怎样的

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转换器库。

© www.soinside.com 2019 - 2024. All rights reserved.