【问题标题】:Android Volley JsonObjectRequest format: http://localhost:8080/xy?param1=1&param2=2Android Volley JsonObjectRequest 格式:http://localhost:8080/xy?param1=1&param2=2
【发布时间】:2018-01-31 19:55:12
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

    标签: android android-volley jsonobjectrequest


    【解决方案1】:

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

    摆脱添加帖子正文,并使用 Uri.Builder.appendQueryParameter(key, value) 在 URL 上手动附加参数。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:
        int p1=1;
        int p2= 2;
        
        string url= "http://localhost:8080/xy?param1="+p1+"&param2="+p2;
        

        把这个网址替换成你正在使用的网址..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-26
          相关资源
          最近更新 更多