【问题标题】:android httpGet issue安卓 httpGet 问题
【发布时间】:2011-03-14 06:12:34
【问题描述】:

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

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";

如何解决网址中的字间距问题?

【问题讨论】:

    标签: android http


    【解决方案1】:

    在这里,我给你一个函数,它将从 url 中删除所有无效字符。请在此函数中传递您的 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;
    }
    

    【讨论】:

    • 据我统计,这会创建大约 30 个 String 对象...总计。
    【解决方案2】:

    你绝对应该使用URLEncoder.encode(String, String)

    【讨论】:

      【解决方案3】:

      我认为问题在于 URL 的参数。无需编码或替换整个 URL,只需像这样对参数进行编码:

      Url: http://myHost.com/mail.do?address=New York city@&time=12$3;
      right url:http://myHost.com/mail.do? + UrlEncode(address) + "&" + UrlEncode(time);
      

      另一个例子是:

      Map<String, String> params = new HashMap<String, String>();
      params.put("email", URLEncoder.encode(loginStr));
      params.put("pass", URLEncoder.encode(passwStr));
      Model.doAuthUser(params, userCallback);
      

      Model.doAuthUser 如下所示:

       String url = "http://myHost.com/mail.do";
       if (params != null) {
       url += "?";
       Boolean beginAddParams = true;
       for (Entry<String, String> entryParams : params.entrySet()) {
       if (!beginAddParams) {
         url +="&";
        } else {
         beginAddParams = false;
        }
        url += entryParams.getKey() + "=" + entryParams.getValue();
       }
      

      并在任何你想要的地方使用url

      【讨论】:

        【解决方案4】:

        我认为你应该使用 %20 而不是空格

        【讨论】:

        • 是的,将“”的字符串替换为 %20。
        • 当你的 url 中只有空格时很好,但是还有很多其他字符,然后给出 IllegalCharacterException。
        【解决方案5】:

        当我将变量发布到我的网络服务时,URLEncoder 对我不起作用,因为它正在替换所有参数。我最终使用了

        URI uri = new URI("http", "server", "/path", "action=send&msg=newmessage", null);
        String url = uri.toASCIIString();
        

        以及稍后使用带有 httpClient 的 httpPost 解析我下载的数据...

        String xml = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
        

        我假设 httpGet 方法会以同样的方式工作。

        【讨论】:

          猜你喜欢
          • 2012-05-07
          • 2018-07-03
          • 2014-02-19
          • 2011-06-12
          • 2016-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多