Android Volley:无法解析构造函数jsonobjectrequest [英] Android Volley: Cannot resolve Constructor jsonobjectrequest

查看:91
本文介绍了Android Volley:无法解析构造函数jsonobjectrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Volley来获取JSON并进行解析.但是android studio表示无法解析构造函数jsonobjetrequest.我不明白这是什么错误.以下是代码.

I am trying to use volley to fetch JSON and parse. But android studio says cannot resolve constructor jsonobjetrequest. I am not able to understand what is the mistake. The following is the code.

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();
                    }
                });

推荐答案

因为您的项目使用的是mcxiaoke的齐射,其中有以下两个构造函数:

Because your project uses mcxiaoke's volley, in which there are two following constructors:

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,则该类将无法知道要使用哪个构造函数.

So if you pass null, there is no way for the class to know which constructor to be used.

如前所述,您可以删除Request.Method.GET或删除null,或铸造诸如(String)null(JSONObject)null.

As commented, you can either remove Request.Method.GET, or remove null, or casting such as (String)null or (JSONObject)null.

P/S:如果您的项目使用Google的官方排球,那么您问题中的构造函数是正确的.

P/S: if your project uses Google's official volley, the constructor in your question is correct.

希望这会有所帮助!

这篇关于Android Volley:无法解析构造函数jsonobjectrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆