3

嗨,在我的 android 项目中,我正在调用 web 服务并通过查询字符串参数发送 get 参数,现在的问题是,如果查询字符串参数值包含任何空格,那么我会收到 505 错误

                    URL url = new URL(urlstring.trim());                        
                    urlConnection = (HttpURLConnection) url.openConnection();
                    int response = urlConnection.getResponseCode();

我有一个疑问,如果我使用URLEncode(urlstring.trim(),"UTF-8")我是否还需要更改我的网络服务代码?

4

1 回答 1

8

您应该只对参数的值进行编码:

    String urlString = "http://test.com?param1=" + URLEncoder.encode(value1, "UTF-8") + "&param2=" + URLEncoder.encode(value2, "UTF-8") ;

    URL url = new URL(urlstring.trim());                        
    urlConnection = (HttpURLConnection) url.openConnection();
    int response = urlConnection.getResponseCode();
于 2012-11-06T14:23:57.133 回答