gpt4 book ai didi

java - http 连接错误 java.lang.IllegalArgumentException : Illegal character in query at index 76

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:23 24 4
gpt4 key购买 nike

我在 http 连接 java.lang.IllegalArgumentException 中遇到错误:索引 76 处的查询中存在非法字符。尝试了所有 3 个示例。无法修复它。尝试了 url_encode 但它没有考虑组件:位置部分。我如何使这个 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>();

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

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;

最佳答案

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

另外,是否定义了newText

关于java - http 连接错误 java.lang.IllegalArgumentException : Illegal character in query at index 76,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com