android httpGet问题

标签 android http

我正在尝试使用以下代码执行以下链接:

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/

相关文章:

android - 在构建 Android 项目时添加 dexdebug 错误

android - 兼容数据类型

javascript - 无法解构属性,因为它是未定义的

http - 我认为我的文件上传连接正在关闭...为什么?

REST Api - 创建资源重定向

http - 如何同时使用 Rejection 和问号运算符来处理 Warp 中的错误?

java - Android 上的遗留 Java 1.3 应用程序

java - 无法从类中获取 int 值到另一个类 : android studio

android - 你如何从Android中用户的推特用户名中找到用户的推特ID

http - 为什么 http.ResponseWriter 不实现响应流 End() 调用?