I am trying to implement a JsonRequestObject with Volley library on android
我想在android上用Volley库实现一个JsonRequestObject
this is the code of the method
这是方法的代码
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();
}
}) {
and this is the error message
这是错误信息
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
错误:(71,34)错误:对JsonObjectRequest的引用与JsonObjectRequest中的构造函数JsonObjectRequest(int,String,String,Listener,ErrorListener)和JsonObjectRequest匹配中的构造函数JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)不一致
1 个解决方案
#1
8
Try passing blank string in constructor instead of null.
尝试在构造函数中传递空字符串而不是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();
}
}) {
The reason it was not working is because when you pass parameters in constructor, it tries to match them with available constructors on three basic criterias:
它不起作用的原因是因为当你在构造函数中传递参数时,它会尝试将它们与三个基本标准上的可用构造函数进行匹配:
- The total number of parameters you have passed.
- 您传递的参数总数。
- The ordering of those parameters like (int, String, int);
- 这些参数的排序如(int,String,int);
- And the type of parameter passed.
- 并且传递了参数的类型。
In your case, based on above conditions it matched to two constructors. The value of String or JSONObject can be null thats why it was showing you ambiguous error on JsonObjectRequest(int,String,String,Listener,ErrorListener) and JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener). We just passed blank String as argument so that it now knows that the third argument is of type string.
在您的情况下,基于上述条件,它与两个构造函数匹配。 String或JSONObject的值可以为null,这就是为什么它在JsonObjectRequest(int,String,String,Listener,ErrorListener)和JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)上显示模糊错误的原因。我们只传递空字符串作为参数,以便它现在知道第三个参数是字符串类型。
#1
8
Try passing blank string in constructor instead of null.
尝试在构造函数中传递空字符串而不是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();
}
}) {
The reason it was not working is because when you pass parameters in constructor, it tries to match them with available constructors on three basic criterias:
它不起作用的原因是因为当你在构造函数中传递参数时,它会尝试将它们与三个基本标准上的可用构造函数进行匹配:
- The total number of parameters you have passed.
- 您传递的参数总数。
- The ordering of those parameters like (int, String, int);
- 这些参数的排序如(int,String,int);
- And the type of parameter passed.
- 并且传递了参数的类型。
In your case, based on above conditions it matched to two constructors. The value of String or JSONObject can be null thats why it was showing you ambiguous error on JsonObjectRequest(int,String,String,Listener,ErrorListener) and JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener). We just passed blank String as argument so that it now knows that the third argument is of type string.
在您的情况下,基于上述条件,它与两个构造函数匹配。 String或JSONObject的值可以为null,这就是为什么它在JsonObjectRequest(int,String,String,Listener,ErrorListener)和JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)上显示模糊错误的原因。我们只传递空字符串作为参数,以便它现在知道第三个参数是字符串类型。