使用凌空
问题描述:
当服务器返回以下JSON为JsonObjectRequest错误没有发现合适的构造 -使用凌空
[ { “USER_ID”:1, “first_name的”: “亚历克斯”, “姓氏”: “goft”, “密码”: “doom00”, “电子邮件”: “[email protected]”, “用户名”: “alexgoft” } ]
我尝试使用凌空它检索用下面的函数 -
public void makeRequest() {
final String HOST = "http://10.0.2.2:3000/";
final String url = HOST + "get_data?type=user&username=alexgoft";
RequestQueue queue = Volley.newRequestQueue(context);
Toast.makeText(context, url, Toast.LENGTH_LONG).show();
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Toast.makeText(context, "In succ", Toast.LENGTH_LONG).show();
try {
returned_json = response.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "In error", Toast.LENGTH_LONG).show();
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
error.getStackTrace();
Log.d("error", error.toString());
}
});
queue.add(getRequest);
}
的问题是,我碰到下面的错误 -
Error:(46, 40) error: no suitable constructor found for JsonObjectRequest(int,String,<anonymous Listener<JSONArray>>,<anonymous ErrorListener>)
constructor JsonObjectRequest.JsonObjectRequest(int,String,Listener<JSONObject>,ErrorListener) is not applicable
(argument mismatch; <anonymous Listener<JSONArray>> cannot be converted to Listener<JSONObject>)
constructor JsonObjectRequest.JsonObjectRequest(String,JSONObject,Listener<JSONObject>,ErrorListener) is not applicable
(argument mismatch; int cannot be converted to String)
什么我需要做的,这样returned_json
将包含返回的JSON从主机?
这是怎么看的IDE -
答
你必须调用JsonArrayRequest
而不是JsonObjectRequest
。
供您参考:https://www.androidhive.info/2014/09/android-json-parsing-using-volley/
答
你需要传递的情况下,空你没有任何数据与您的要求
注传递下去:既然你的回应是JSONArray
,所以你需要实现JsonArrayRequest
代替JsonObjectRequest
如也由@Krupa Kakkad
new JsonObjectRequest(Request.Method.GET, url, null,
// ^^^^
new Response.Listener<JSONArray>() {
指出
@param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
* indicates no parameters will be posted along with request.
构造签名
公共JsonObjectRequest(INT方法,字符串URL,JSONObject的 jsonRequest, 监听器监听器,ErrorListener errorListener){
或
公共JsonObjectRequest (String url,JSONObject jsonRequest, 监听器监听, ErrorListener errorListener){
等待,如果有编译时错误,那么怎么来回应? –