java.lang.IllegalArgumentException:索引 59 处查询中的非法字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27126839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalArgumentException: Illegal character in query at index 59
提问by Devrath
What i am doing:I am trying to make a reverse geocoding in android
我在做什么:我正在尝试在 android 中进行反向地理编码
I am getting error as::java.lang.IllegalArgumentException: Illegal character in query at index 59: http://maps.google.com/maps/api/geocode/json?address=Agram, Bengaluru, Karnataka, India&sensor=false
我收到错误为::java.lang.IllegalArgumentException:索引 59 处查询中的非法字符:http: //maps.google.com/maps/api/geocode/json?address= Agram, Bengaluru, Karnataka, India&sensor=false
NOte: that request gets a json response in browser but not from my class below
注意:该请求在浏览器中获得 json 响应,但不是来自我下面的课程
This line is giving this error::
这一行给出了这个错误::
HttpGet httpget = new HttpGet(url);
JSONfunctions.java
JSONfunctions.java
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
采纳答案by laalto
Use URLEncoder.encode()
to encode the value of your address
parameter "Agram, Bengaluru, Karnataka, India"
before putting it in the URL string so that it becomes something like
用于在将参数URLEncoder.encode()
值放入 URL 字符串之前对其进行编码,使其变成类似address
"Agram, Bengaluru, Karnataka, India"
http://maps.google.com/maps/api/geocode/json?address=Agram,+Bengaluru,+Karnataka,+India&sensor=false
i.e. spaces changed to +
and other special octets represented as %xx
.
即空格更改为+
和其他特殊八位字节表示为%xx
。
Browsers do smart URL encoding for strings entered in the address bar automatically so that's why it works there.
浏览器会自动对地址栏中输入的字符串进行智能 URL 编码,这就是它在那里工作的原因。
回答by Akash Moradiya
Build your url like,
构建您的网址,例如,
final StringBuilder request = new StringBuilder(
"http://maps.googleapis.com/maps/api/geocode/json?sensor=false");
request.append("&language=").append(Locale.getDefault().getLanguage());
request.append("&address=").append(
URLEncoder.encode(locationName, "UTF-8"));
回答by Nirendra Singh Panwar
I am using httpclient 4.3.3
我正在使用 httpclient 4.3.3
String messagestr = "Welcome to Moqui World";
String url="http://my.example.com/api/sendhttp.phpauthkey="+URLEncoder.encode("17djssnvndkfjb110d3","UTF-8")+"&mobiles=91"+URLEncoder.encode(contactNumber,"UTF-8")+"&message="+URLEncoder.encode(messagestr,"UTF8")+"&sender="+URLEncoder.encode("WMOQUI","UTF-8")+"&route=4";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
It's working fine for me. I hope this may help you.
它对我来说很好用。我希望这可以帮助你。