Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从服务器获得505响应_Java_Android_Apache_Http Status Code 505 - Fatal编程技术网

Java 从服务器获得505响应

Java 从服务器获得505响应,java,android,apache,http-status-code-505,Java,Android,Apache,Http Status Code 505,嗨,在我的android项目中,我调用了一个Web服务,并通过查询字符串参数发送get参数,现在的问题是,若查询字符串参数值包含任何空格,那个么我将得到505个错误 URL url = new URL(urlstring.trim()); urlConnection = (HttpURLConnection) url.openConnection();

嗨,在我的android项目中,我调用了一个Web服务,并通过查询字符串参数发送get参数,现在的问题是,若查询字符串参数值包含任何空格,那个么我将得到505个错误

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

如果我使用
URLEncode(urlstring.trim(),“UTF-8”)
我是否也需要更改我的Web服务代码?

您应该只编码参数的值:

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

您应该只编码参数的值,而不是完整的url谢谢fiddler的快速响应,我正在编码完整的url。我在Glassfish 5上的rest服务中犯了同样的错误。我对整个url进行了编码,得到了错误的url异常,我们必须只对参数的值进行编码。