【发布时间】: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;
}
【问题讨论】:
-
JsonObjectRequest 需要一个 JSONObject 作为响应,但您正在发送一个 JSONArray。要么将数组包装在服务器端的 JSONObject 中,要么使用 StringRequest 接收字符串并将其解析为 JSONArray。
标签: android json http android-volley