英文:
Android studio: dont work JsonArrayRequest
问题
请帮帮我(我不明白问题出在哪里。它显示一个错误和文字“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"}]}
英文:
help me please ( I dont understand what the problem is. It gives an error and the text "ERROR", I can not find anything in the logs. I am a beginner and am currently studying on android development.
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"}]}
</details>
# 答案1
**得分**: 0
以下是翻译好的代码部分:
```java
// 仅需转移部分代码
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);
英文:
It was only necessary to transfer part of the code
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语言的经验与见解,让所有开发者获益!
评论