请帮助我(我不明白问题是什么。它给出了一个错误和文本“ERROR”,我在日志中找不到任何内容。我是初学者,目前正在学习 android 开发。
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, API + 0, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
TextView textWord = findViewById(R.id.textWord);
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
String word = jsonObject.getString("word");
textWord.setText(word);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
TextView textWord = findViewById(R.id.textWord);
textWord.setText("ERROR");
}
});
requestQueue.add(jsonArrayRequest);
{"Words":[{"word":"test"}]}
最佳答案
只需要转移部分代码
RequestQueue requestQueue = Volley.newRequestQueue(this);
final TextView textWord = findViewById(R.id.textWord);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, API + 0, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
String word = jsonObject.getString("word");
textWord.setText(word);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textWord.setText("ERROR");
}
});
requestQueue.add(jsonArrayRequest);
关于java - Android工作室: dont work JsonArrayRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61098627/