排球库中JsonObjectRequest出错

Fak*_*her 1 android json android-volley

我试图在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();
                }
            }) {
Run Code Online (Sandbox Code Playgroud)

这是错误信息

错误:(71,34)错误:对JsonObjectRequest的引用与JsonObjectRequest中的构造函数JsonObjectRequest(int,String,String,Listener,ErrorListener)和JsonObjectRequest匹配中的构造函数JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)不一致

Vik*_*alp 8

尝试在构造函数中传递空字符串而不是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();
            }
        }) {
Run Code Online (Sandbox Code Playgroud)

它不起作用的原因是因为当你在构造函数中传递参数时,它会尝试将它们与三个基本标准上的可用构造函数进行匹配:

  1. 您传递的参数总数.
  2. 这些参数的排序如(int,String,int);
  3. 并且传递了参数的类型.

在您的情况下,基于上述条件,它与两个构造函数匹配.String或JSONObject的值可以为null,这就是为什么它在JsonObjectRequest(int,String,String,Listener,ErrorListener)和JsonObjectRequest(int,String,JSONObject,Listener,ErrorListener)上显示模糊错误的原因.我们只是将空字符串作为参数传递,以便它现在知道第三个参数是string类型.