android httpGet问题

时间:2011-03-14 06:12:34

标签: 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)

鉴于以下网址没有错误,“msg =”

的字词没有空白
String url="http://<server>/<path>/action=send&msg=newmessage";

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

5 个答案:

答案 0 :(得分:15)

在这里,我给你一个功能,将从网址中删除所有无效字符。请在此函数中传递您的网址,您将获得一个带编码字符串的新网址。

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

答案 1 :(得分:12)

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

答案 2 :(得分:5)

我认为问题在于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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

当我将变量发布到我的webservice时,URLEncoder对我来说没有用,因为它正在替换所有参数。我最终使用

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

以及之后使用httpClient和httpClient解析我下载的数据...

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方法的工作方式相同。