解决方案 »

  1.   

    url不可能出现空格那看来是参数出现空格 可以使用URLEncoder.encode来处理指定参数
      

  2.   

    HttpGet get = new HttpGet(URLEncoder.encode(url,"UTF-8"));
      

  3.   


    该怎么Encode呢?是把整个url都放进去吗?我刚才试了一下,大概是这样的:
    (为了保密,我把网址换成了baidu)String url="http://baidu.com?name=han&content=hello world";
    HttpGet get = new HttpGet(URLEncoder.encode(url,"UTF-8"));然后报异常了:
    java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://baidu.com?name=han&content=hello world是不是不应该把整个url给Encode了啊?如果只Encode后面的请求语句的话,再怎么和前面的http://baidu.com拼在一起呢?
      

  4.   

    URLEncoder.encode().这个方法把所有非字母数字字符改变为%序列(除了空格、下划线、连字符、点和星号),如果把整个url给encode的话,=,&这些字符也会编码。所以最好逐部分编码。String query = URLEncode.encode("name");
    query += "=";
    query += URLEncode.encode("han");
    query += "&";
    query += URLEncode.encode("content");
    query += "=";
    query += URLEncode.encode("hello world");String url = "http://baidu.com?"+query;
      

  5.   

    比如 http://baidu.com?name=han&content=hello world则这样处理
    url = “http://baidu.com?name=han&content=”+URLEncoder.encode(“hello world”,“utf-8”);
      

  6.   

    为什么不用post?get的url有长度的限制,接不了多少字
      

  7.   

    在执行之前写上 url = url.replaceAll(" ", "%20");  把空格替换掉
      

  8.   

    不对啊,用URLEncode.encode("hello world!", "UTF-8");之后的结果是hello+world啊,而不是hello%20world啊
    有人知道怎么做才正确么,不用replaceall的话(感觉不太科学啊)