gpt4 book ai didi

android - 改造错误 : Expected BEGIN_ARRAY but was STRING

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:22 24 4
gpt4 key购买 nike

在api响应中有时可以是数组,有时可以是字符串。

这里的细节是Array

 {  "ts": "2015-06-16 11:28:33","success": true,"error": false,"details": [
{
"user_id": "563",
"firstname": "K.Mathan"
},
{
"user_id": "566",
"firstname": "Surya"
},
{
"user_id": "562",
"firstname": "Idaya"
} ]}

有时细节可以是字符串

{  "ts": "2015-06-16 11:28:33",
"success": true,
"error": false,
"details": "no data" }

这里的details是String

如何从这种类型的响应中获取值(value)

我现在的声明是

  @SerializedName(value="details")
public List<detailslist> details ;

谁能帮我找到解决方案?

最佳答案

您尝试过使用原始响应类型吗?

@GET("your_url")
void getDetails(Callback<Response> cb);

然后您可以像这样使用 JSONObject 和 JSONArray 解析响应:

    Callback<Response> callback = new Callback<Response>() {
@Override
public void success(Response detailsResponse, Response response2) {

String detailsString = getStringFromRetrofitResponse(detailsResponse);

try {
JSONObject object = new JSONObject(detailsString);

//In here you can check if the "details" key returns a JSONArray or a String

} catch (JSONException e) {

}

}

@Override
public void failure(RetrofitError error) {

});

getStringFromRetrofitRespone 可能在哪里:

public static String getStringFromRetrofitResponse(Response response) {
//Try to get response body
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {

reader = new BufferedReader(new InputStreamReader(response.getBody().in()));

String line;

try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();

}

关于android - 改造错误 : Expected BEGIN_ARRAY but was STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30869276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com