功能:售后地址的验证 当用户点击某地址时,将地址信息传递到后台,再从后台调用接口,判断用户地址填写是否正确
解析地址功能的实现层代码(简化过的)
publicvoidaddressResolution(String address, String id) {
String addresses ="{\"id\":\""+ id + "\",\"address\":\""+ address +"\"}";//将传递的两个值赋值给string
try{
String response=HttpClientUtil.doGetUrl(http://baidu.com?addresses=["+ addresses +"]");//注意:在发送请求时一定要填写http://或https://
} catch (Exception e) {
e.printStackTrace();
}
}
在执行之后发生的异常信息
java.lang.IllegalArgumentException: Illegal character in query at index 82: http://10.138.225.240:10061/internalapi/hsi/analyticSuperMapAPI/fendan?addresses=[{"id":"111","address":"山东省日照市岚山区"}]
at java.net.URI.create(URI.java:852)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
.....................................................
2018-04-19 09:09:10.575 ERROR 7892 --- [tp1021786447-94] c.h.h.e.s.w.WorkOrderServiceImpl : 地址分单异常信息:java.lang.IllegalArgumentException: Illegal character in query at index 82: http://10.138.225.240:10061/internalapi/hsi/analyticSuperMapAPI/fendan?addresses=[{"id":"111","address":"山东省日照市岚山区"}]
异常原因:更确切的讲,当URL地址里包含非西欧字符的字符串时,浏览器都会将这些非西欧字符串转换成application/x-www-form-urlencoded MIME 字符串。在开发过程中,我们可能涉及将普通字符串和这种特殊字符串的相关转换,这就需要使用URLDecoder和URLEncoder类进行实现,其中:
URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串;
URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将普通字符串转换成application/x-www-form-urlencoded MIME字符串。
所以在这里使用URLEncoder方法
导包 import java.net.URLEncoder;
publicvoidaddressResolution(String address, String id) {
String addresses ="{\"id\":\""+ id + "\",\"address\":\""+ address +"\"}";
String addresses =URLEncoder.encode(param); //url中有汉字或特殊字符(非字母和数字的字符),实现 转码
try{
String response = HttpClientUtil.doGetUrl(http://baidu.com?addresses=["+ addresses +"]");
} catch (Exception e) {
e.printStackTrace();
}
}
执行成功