【问题标题】:Android Volley: Cannot resolve Constructor jsonobjectrequestAndroid Volley:无法解析构造函数 jsonobjectrequest
【发布时间】:2015-10-30 04:03:56
【问题描述】:

我正在尝试使用 volley 来获取 JSON 并进行解析。但 android studio 说无法解析构造函数 jsonobjetrequest。我无法理解错误是什么。以下是代码。

JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try{
                            JSONArray routes = response.getJSONArray("routes");
                        }
                        catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

【问题讨论】:

  • 你的项目用的是谷歌的volley还是mcxiaoke的volley?
  • 我正在使用 android SDK 进行开发。有 import com.android.volley.Request;正在使用。
  • 尝试以下两个选项之一:删除Request.Method.GET,或删除null
  • 是的。它是如何工作的。
  • 谢谢。我会检查并理解

标签: android json android-volley


【解决方案1】:

因为你的项目使用mcxiaoke的volley,其中有以下两个构造函数:

public JsonObjectRequest(int method, String url, String requestBody,
                             Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, requestBody, listener,
                errorListener);
    }

public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

所以如果你传递null,类就没有办法知道要使用哪个构造函数。

根据评论,您可以删除 Request.Method.GET,或删除 null,或强制转换,例如 (String)null(JSONObject)null

P/S:如果你的项目使用的是谷歌官方的凌空,那么你问题中的构造函数是正确的。

希望这会有所帮助!

【讨论】:

  • 好的。现在更清楚了。因为我是从“code.tutsplus.com/tutorials/…”复制过来的。如何知道它的 mcxiaoke's 或 googles。推荐哪个
  • 对于mcxiaoke:你会在build.gradle文件中找到compile 'com.mcxiaoke.volley:library:1.0.17'这样的行
  • 如果你想要谷歌的凌空,请从这里阅读developer.android.com/training/volley/index.html
  • 为什么android默认使用mcxiaoke。如果我想换成谷歌怎么办
  • 不,这不是默认设置。只是因为您从使用该库的项目中复制。如果想改成谷歌的,请阅读谷歌的文档作为我上面的链接
猜你喜欢
  • 1970-01-01
  • 2017-02-28
  • 2013-12-13
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多