我正在尝试使用以下代码执行以下链接:
class httpget{
HttpGet httpGet=null;
public void linkexecute(){
String url="http://<server>/<path>/action=send&msg=new message";
httpGet= new HttpGet(url); // line 1
....
}
at line 1 it is giving error "Illegal arguement exception"
java.lang.IllegalArgumentException: Illegal character in query at index 77: http://<server>/<path>/sms.json?action=send&msg=new message
at java.net.URI.create(URI.java:970)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.sms.login.LoginService.sendSms(LoginService.java:143)
鉴于下面给定的 URL 没有错误,它在 "msg="的单词中没有间隙
String url="http://<server>/<path>/action=send&msg=newmessage";
如何解决 URL 中的单词间隙问题?
最佳答案
在这里,我为您提供了一个函数,可以从 url 中删除所有无效字符。请在此函数中传递您的网址,您将获得一个带有编码字符串的新网址。
public static String convertURL(String str) {
String url = null;
try{
url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28").replace(")", "%29")
.replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23").replace("$", "%24")
.replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
.replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
.replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
}catch(Exception e){
e.printStackTrace();
}
return url;
}
关于android httpGet问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5295347/