【问题标题】:How do I get JSON array list from the Server?如何从服务器获取 JSON 数组列表?
【发布时间】:2017-02-08 11:58:18
【问题描述】:

我在我的 android 客户端中使用 Volley 从服务器检索一些 JSON 值。 为了获得价值,我做了以下事情:

@Override
            public void onClick(View v) {
                queue = Volley.newRequestQueue(MapsActivity.this);
                request = new JsonObjectRequest(
                        Request.Method.GET, // the request method
                        "http://10.0.2.2:8080/SomeURL",
                        new JSONObject(map),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response){

                                textView = (TextView)findViewById(R.id.textView);
                                textView.setText(response.toString());
                            }
                        },
                        new Response.ErrorListener() { // the error listener
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                textView = (TextView)findViewById(R.id.textView);
                                textView.setText("Error::"+ error.toString());
                            Toast.makeText(getApplicationContext(), "Error:" + error.toString(), Toast.LENGTH_LONG).show();
                            }
                        });

                queue.add(request);
            }

        });

在服务器端,我返回的值如下:

  @GET
    @Produces("application/json")
    public List<Employee> getEmployees() {
        EmployeeDAO dao = new EmployeeDAO();
        List employees = dao.getEmployees();
        return employees;
    }

但是,我得到了“onErrorResponse()”中的值

【问题讨论】:

  • JsonObjectRequest 需要一个 JSONObject 作为响应,但您正在发送一个 JSONArray。要么将数组包装在服务器端的 JSONObject 中,要么使用 StringRequest 接收字符串并将其解析为 JSONArray。

标签: android json http android-volley


【解决方案1】:

您正在等待代码中的JSONObject,请将JsonObjectRequest 更改为JsonArrayrequest。在onResponse(JSONArray response) 你解析响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多