【发布时间】:2020-11-20 04:13:59
【问题描述】:
我能够将 JSON 数组解析为文本视图,但想要填充微调器。目前我正在使用以下代码将 JSON 解析为文本视图。理想情况下,我希望有一个微调器,它按“名称”列出结果,并根据所选项目将“体积”、“全重”和“空重”放入单独的文本视图中。这是我第一次尝试在应用中包含微调器,但我不知道从哪里开始。
public void getJSON(){
String url = "https://example.com/DBReadJSON.php";
JsonRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("bottles");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject bottles = jsonArray.getJSONObject(i);
String Name = bottles.getString("name");
int vol = bottles.getInt("volume");
int full = bottles.getInt("fullweight");
int empty = bottles.getInt("emptyweight");
mTextViewResult.append(Name + ", " + vol + ", " + full +", " + empty + "\n");
Toast.makeText(getApplicationContext(),Name, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
我意识到这可能是一个愚蠢的问题,我已经完全错误地解析了 JSON。对于那个很抱歉。我什至会很感激有人指出如何做到这一点的教程的方向。
谢谢。
【问题讨论】: