连接错误:java.lang.IllegalArgumentException: 查询中的非法字符,索引为76。

3
我在http连接中遇到了一个错误:java.lang.IllegalArgumentException: Illegal character in query at index 76. 我尝试了所有三个示例,但无法解决。我尝试使用url_encode,但它没有考虑到组件:locality部分。我该如何使这个URL正常工作?谢谢提前。
BufferedReader in = null;

            HttpClient httpclient = new DefaultHttpClient();

            HttpGet request = new HttpGet();
            URI website = new URI("http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane|country=us|administrative_area:washington&address="+newText+"&sensor=false");
            request.setURI(website);
            HttpResponse response = httpclient.execute(request);
            in = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            String line = in.readLine();

            data = line;
            suggest = new ArrayList<String>();

And

                            String uri =("http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane|country=us|administrative_area:washington&address="+newText+"&sensor=false");

                            uri=URLEncoder.encode(uri,"UTF-8");

                            HttpClient hClient = new DefaultHttpClient();
                            HttpGet hGet = new HttpGet(uri);
                            ResponseHandler<String> rHandler = new BasicResponseHandler();
                            data = hClient.execute(hGet,rHandler);
                            suggest = new ArrayList<String>();

And

String urlstr="http://maps.googleapis.com/maps/api/geocode/json?";
                String str_parameters = "components=locality:Spokane|country=us|administrative_area:washington";
                String encodedparams = URLEncoder.encode(str_parameters,"UTF-8")+"&address="+URLEncoder.encode(newText, "UTF-8")+"&sensor="+URLEncoder.encode("false", "UTF-8");

                String str_finalurl=urlstr+encodedparams;
1个回答

7
第76个字符是|,可能是问题的原因。你尝试用十六进制格式%7C替换它吗?形成这样的格式:http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane%7Ccountry=us%7Cadministrative_area:washington&address="+newText+"&sensor=false
此外,newText是否已定义?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接