【问题标题】:Compililation Error while using JsonObjectRequest使用 JsonObjectRequest 时出现编译错误
【发布时间】:2015-03-17 17:00:01
【问题描述】:

我正在使用 mcxiaoke/android-volley 库。我收到编译错误

Error:(77, 37) error: reference to JsonObjectRequest is ambiguous, both constructor 
JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener) in JsonObjectRequest and constructor 
JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener) in JsonObjectRequest match

这是我的代码。我不知道出了什么问题。任何帮助表示赞赏

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            getRequestUrl(10),
            null,
            new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

【问题讨论】:

  • 你正在传递一个空值,传递一个字符串或一个 JSONObject
  • 我认为空值是可以接受的。
  • 是的 null 被接受,但它不知道你正在调用哪个构造函数。将 null 转换为字符串

标签: android android-volley


【解决方案1】:

将 null 转换为字符串或 JSONObject,我认为它应该可以正常工作。

new JsonObjectRequest(Request.Method.GET,
            getRequestUrl(10),
            (String)null,
            new Response.Listener<JSONObject>()

【讨论】:

  • 仅传递 null 在 Eclipse 中有效,但在 android studio 上我遇到了该错误,您的解决方案解决了我的问题,谢谢:)
【解决方案2】:

比尔盖茨是对的,如果你传入 null 而不是它在其中一个构造函数中期望的 String 或 JSONObject 类型的 Object,那么该类无法知道要使用哪个构造函数,否则你会得到这个模棱两可的错误,表示构造函数有 2 个匹配项。

试试:

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
        getRequestUrl(10),
        "",
        new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {


    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

【讨论】:

    【解决方案3】:

    你刚刚使用了空引用。

    new JsonObjectRequest(Request.Method.GET,
            getRequestUrl(10),
            (String)null,
            new Response.Listener<JSONObject>()
    

    它对我有用

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2013-06-16
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多