嗨,在我的android项目中我调用webservice并通过查询字符串参数发送get参数,现在问题是如果查询字符串参数值包含任何空格,那么我得到505错误
URL url = new URL(urlstring.trim());
urlConnection = (HttpURLConnection) url.openConnection();
int response = urlConnection.getResponseCode();
我怀疑是否使用URLEncode(urlstring.trim(),"UTF-8")
我是否还需要更改我的网络服务代码?
答案 0 :(得分:7)
您应该只编码参数的值:
String urlString = "http://test.com?param1=" + URLEncoder.encode(value1, "UTF-8") + "¶m2=" + URLEncoder.encode(value2, "UTF-8") ;
URL url = new URL(urlstring.trim());
urlConnection = (HttpURLConnection) url.openConnection();
int response = urlConnection.getResponseCode();