Android Studio:不起作用的 JsonArrayRequest

huangapple 未分类评论49阅读模式
英文:

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&lt;JSONArray&gt;() {
        @Override
        public void onResponse(JSONArray response) {
            TextView textWord = findViewById(R.id.textWord);
            for (int i = 0; i &lt; response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    String word = jsonObject.getString(&quot;word&quot;);
                    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(&quot;ERROR&quot;);
        }
    });
    requestQueue.add(jsonArrayRequest);
      {&quot;Words&quot;:[{&quot;word&quot;:&quot;test&quot;}]}

</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&lt;JSONArray&gt;() {
    @Override
    public void onResponse(JSONArray response) {
        for (int i = 0; i &lt; response.length(); i++) {
            try {
                JSONObject jsonObject = response.getJSONObject(i);
                String word = jsonObject.getString(&quot;word&quot;);
                textWord.setText(word);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        textWord.setText(&quot;ERROR&quot;);
    }
});
requestQueue.add(jsonArrayRequest);

huangapple
  • 本文由 发表于 2020年4月8日 18:33:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61098627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定