【问题标题】:JSON Object Request GET MethodJSON 对象请求 GET 方法
【发布时间】:2018-12-18 03:44:58
【问题描述】:

为什么我不能在 url 中发送参数,字符串在 JSON 对象请求中包含多个单词?

当我尝试使用字符串“haha”发送参数时,它可以工作,但是当我尝试使用字符串“haha haha​​”(单词之间有空格)发送参数时,它会调用 onErrorResponse 函数。

下面是我的代码:

String url = String.format("http://172.xx.x.xx:xxxxx/api/users?name=%s", nama);

JsonObjectRequest objectRequest = new JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

有什么办法让我的url可以接收字符串参数中包含多个单词的字符串参数??

【问题讨论】:

  • 请尝试像new URLEncoder().encode(nama, Charsets.UTF_8)一样编码nama

标签: java android json jsonobjectrequest


【解决方案1】:

您也可以使用它,因为当您发送请求时,空间会被 %20 重新定位 String url = String.format("http://172.xx.x.xx:xxxxx/api/users?name=%s", nama.replace(" ","%20"));

【讨论】:

    【解决方案2】:

    试试这个

    String nama = "haha haha";
    String url = "http://172.xx.x.xx:xxxxx/api/users?name="+nama;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-18
      • 2016-10-02
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2018-01-06
      相关资源
      最近更新 更多