volley 库中的 JsonObjectRequest 出错
Error in JsonObjectRequest in volley library
我正尝试在 android
上使用 Volley 库实现 JsonRequestObject
这是方法的代码
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
这是错误信息
Error:(71, 34) error: reference to JsonObjectRequest is ambiguous
both constructor JsonObjectRequest(int,String,String,Listener,ErrorListener) in JsonObjectRequest and constructor JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener) in JsonObjectRequest match
尝试在构造函数中传递空白字符串而不是 null。
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, "",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
它不起作用的原因是当您在构造函数中传递参数时,它会尝试根据三个基本标准将它们与可用的构造函数相匹配:
- 您传递的参数总数。
- 这些参数的排序,例如 (int, String, int);
- 以及传递的参数类型。
在您的情况下,根据上述条件,它匹配了两个构造函数。 String 或 JSONObject 的值可以为空,这就是为什么它在 JsonObjectRequest(int,String,String,Listener,ErrorListener) 和 JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener) 上向您显示模棱两可的错误。我们刚刚将空白字符串作为参数传递,以便它现在知道第三个参数是字符串类型。
我正尝试在 android
上使用 Volley 库实现 JsonRequestObject这是方法的代码
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
这是错误信息
Error:(71, 34) error: reference to JsonObjectRequest is ambiguous both constructor JsonObjectRequest(int,String,String,Listener,ErrorListener) in JsonObjectRequest and constructor JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener) in JsonObjectRequest match
尝试在构造函数中传递空白字符串而不是 null。
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, "",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
它不起作用的原因是当您在构造函数中传递参数时,它会尝试根据三个基本标准将它们与可用的构造函数相匹配:
- 您传递的参数总数。
- 这些参数的排序,例如 (int, String, int);
- 以及传递的参数类型。
在您的情况下,根据上述条件,它匹配了两个构造函数。 String 或 JSONObject 的值可以为空,这就是为什么它在 JsonObjectRequest(int,String,String,Listener,ErrorListener) 和 JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener) 上向您显示模棱两可的错误。我们刚刚将空白字符串作为参数传递,以便它现在知道第三个参数是字符串类型。