xiaoyan_12
xiaoyan_12
2013-05-29 06:34
采纳率: 84.9%
浏览 2.4k
已采纳

给HTTP链接的一个字符串中删除空白

我想发送一个查询的url:

String url = String.format(
   "http://xxxxx/xxx/xxx&message=%s",myEditBox.getText.toString());
// Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httpclient.getCookieStore().addCookie(cooki);
try {
   ResponseHandler<String> responseHandler = new BasicResponseHandler();
   httpclient.getParams().setParameter("http.connection-manager.timeout", 15000);
   String response = httpclient.execute(httppost, responseHandler);

但是有错误:illegal character at query。可能就是空白的那部分。如何处理这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • nana4ever
    nana4ever 2013-05-30 02:16
    已采纳

    你需要编码 url

    String query = URLEncoder.encode(myEditBox.getText.toString(), "utf-8");
    String url = "http://stackoverflow.com/search?q=" + query;
    
    点赞 评论
  • zxd_72
    zxd_72 2013-05-30 02:20

    使用Try .trim() 可以从 edittext 中获取值。空白来自 edittext ,同时也使用了 "utf-8"

    String value = URLEncoder.encode(myEditBox.getText.toString().trim(), "utf-8");
    String url = "http://xxxxx/xxx/xxx&message=%s" + value;
    
    点赞 评论
  • franzhong
    franzhong 2013-05-30 02:24
    String url    =   String.format(
                "http://xxxxx/xxx/xxx&message=%s",myEditBox.getText.toString());    
    String regEx  =   "\\s"; 
    Pattern   p   =   Pattern.compile(regEx);    
    url           =   p.matcher(url).replaceAll("").trim();    
    
    点赞 评论

相关推荐