使用retrofit 2获得简单的json对象

[英]get simple json object using retrofit 2


My api prints a simple json object like this:

我的api输出一个简单的json对象,如下所示:

{"status":1}

How to say that retrofit (v2) pass it to gson and return an JSONObject contains this key-value pair in response?

如何说更新(v2)传递给gson并返回一个JSONObject包含这个键值对作为响应?

I tried following:

我试着:

Call<JSONObject> result();

But when prints response.body() in onResponse (Response<JSONObject> response) method, it returns {} which means it's empty.

但是当在onResponse (Response Response)方法中打印Response .body()时,它返回{},这意味着它是空的。

2 个解决方案

#1


29  

After lots of R&D I got answer. Please find it below

经过大量的研发,我得到了答案。请在下面找到它

Use JsonObject from package com.google.gson instead of JSONObject from package org.json

使用来自package com.google的JsonObject。gson而不是来自package org.json的JSONObject

After that call Call<JsonObject> result() and in onResponse (Response<JsonObject> response) method used to call response.body() or response.body().toString(); it wil print correct Json from apiwhatever you want

在调用 result()和onResponse (Response Response)方法后,用于调用Response .body()或Response .body().toString();它将从apiany打印出正确的Json

#2


-1  

To get org.json.JSONArray or org.json.JSONObject just create custom ConverterFactory like here: https://github.com/marcinOz/Retrofit2JSONConverterFactory

org.json。获取或org.json。JSONObject只创建自定义转换器工厂,如下所示:https://github.com/marcinOz/Retrofit2JSONConverterFactory

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/09/10/6f4b359b709a934c36f850333d48154f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告