繁体   English   中英

Android 工作室:不工作 JsonArrayRequest

[英]Android studio: dont work JsonArrayRequest

请帮助我(我不明白问题是什么。它给出了一个错误和文本“错误”,我在日志中找不到任何东西。我是一个初学者,目前正在研究 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM