2 xiaoyan 12 xiaoyan_12 于 2013.05.29 14:34 提问

给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 10: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 10: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 10: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();    
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!