Android Volley JsonObjectRequest 格式:http://localhost:8080/xy?param1=1&param2=2

Android Volley JsonObjectRequest format: http://localhost:8080/xy?param1=1&param2=2

我正在尝试使用以下格式发送参数的 Volley JsonObjectRequest (GET):

http://localhost:8080/xy?param1=1&param2=2

我的问题是,如果 param1 为“1”且 param2 为“2”,我应该得到响应代码 200(正常)。但是我总是得到错误的响应代码。 所以我认为,请求以错误的格式发送。

Map<String, String> params = new HashMap();
            params.put("param1", "1");
            params.put("param2", "2");
            JsonObjectRequest jsObjRequest = new JsonObjectRequest
                    (Request.Method.GET, "http://localhost:8080/xy", new JSONObject(params), new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {

                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // TODO Auto-generated method stub

                        }
                    });

            // Access the RequestQueue through your singleton class.
            QueueSingleton.getInstance(LoginActivity.this).addToRequestQueue(jsObjRequest);

谢谢!

现在,您提供的 JsonObject(params) 作为请求的主体,这是不正确的。我不认为 Volley 会在 GET 请求中将你提供的 JSON 对象附加到你的 URL 中......所以你需要自己做。

摆脱添加 post 主体,并使用 Uri.Builder.appendQueryParameter(key, value).

在 URL 上手动附加参数

因为您使用 GET 方法,请尝试使用 url 附加参数,例如

int param1Value = 1, param2Value = 2;
String url = "http://localhost:8080/xy?param1=" + param1Value + "&param2=" + param2Value;
int p1=1;
int p2= 2;

string url= "http://localhost:8080/xy?param1="+p1+"&param2="+p2;

用这个 url 代替您正在使用的 url..