gpt4 book ai didi

android - 不显示带有响应的传入数据

转载 作者:行者123 更新时间:2023-11-29 23:28:11 25 4
gpt4 key购买 nike

我正在为 android 编写应用程序并使用 volley 库。我需要将接收到的数据写入TextResult。怎么做?

private void jsonParse() {
String url = "https://api.apixu.com/v1/current.json?key=...&q=Paris";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("location");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject location = jsonArray.getJSONObject(i);
String name = location.getString("name");
String region = location.getString("region");
String country = location.getString("country");
TextResult.append(name + ", " + region + ", " + country + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}

Json响应示例

{"location":{"name":"Paris","region":"Ile-de-France","country":"France"}}

最佳答案

使用这段代码。

@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = response.getJSONObject("location");

for (int i = 0; i < jsonArray.length(); i++) {
JSONArray location = jsonObject.getJSONArray(i);
String name = location.getString("name");
String region = location.getString("region");
String country = location.getString("country");
TextResult.append(name + ", " + region + ", " + country + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}

关于android - 不显示带有响应的传入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140365/

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