You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用com.android.volley的JsonObjectRequest发起GET请求遇参数异常问题

解决Volley JsonObjectRequest构造方法参数不匹配问题

看起来你遇到的核心问题是Volley版本构造方法的差异以及缺少必要的类导入,下面一步步帮你解决:

1. 修正构造方法参数顺序 & 补全必要参数

你当前的代码参数顺序错误,而且缺少ErrorListener(Volley官方提供的JsonObjectRequest构造方法都需要这个参数)。另外,你需要导入com.android.volley.Request才能使用Request.Method.GET常量。

修正后的代码如下:

// 补充必要的导入
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

// ... 你的其他代码 ...

try {
    String url = getString(R.string.getAddress);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
            Request.Method.GET, // 第一个参数:请求方法
            url, // 第二个参数:请求URL
            null, // 第三个参数:请求体(GET请求传null即可)
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray array = response.getJSONArray("games");
                        JSONArray releventGames = new JSONArray();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject game = array.getJSONObject(i);
                            String homeTeam = game.getString("hometeam");
                            String awayTeam = game.getString("awayteam");
                            if (homeTeam.contains(teamName) || awayTeam.contains(teamName)) {
                                releventGames.put(game);
                            }
                        }
                        // 这里可以处理筛选后的releventGames数据
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("getGames", "解析JSON失败", e);
                    }
                }
            },
            new Response.ErrorListener() { // 必须添加的错误监听
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("getGames", "请求数据失败", error);
                    error.printStackTrace();
                }
            }
    );
    // 记得把请求加入Volley请求队列(需提前初始化RequestQueue)
    // queue.add(jsonObjectRequest);
} catch (Exception e) {
    Log.i("getGames","获取比赛数据失败");
    e.printStackTrace();
}

2. 问题根源解释

  • 构造方法差异:你查阅的官方文档可能对应Volley的较新版本(比如2.x+),但你使用的1.1.x版本的JsonObjectRequest带请求方法的构造顺序是:(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener),而不是URL在前。
  • 缺少Request类导入:Request.Method.GET是com.android.volley.Request类中的常量,你之前没有导入这个类,所以IDE无法识别该常量,导致报错。
  • 缺少ErrorListener:官方提供的JsonObjectRequest构造方法都需要传入ErrorListener,忽略它会导致参数不匹配的编译错误。

3. 额外注意事项

  • 确保你已经初始化了Volley的请求队列(RequestQueue),否则调用queue.add(jsonObjectRequest)会触发空指针异常。初始化代码示例:
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    
  • 如果你不想手动指定请求方法,可以使用简化的构造方法(适用于GET/POST):当请求体jsonRequest为null时,默认使用GET方法;传入JSONObject时默认使用POST方法:
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
            url,
            null,
            responseListener,
            errorListener
    );
    

内容的提问来源于stack exchange,提问作者Adam Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:02:42