给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。可能就是空白的那部分。如何处理这个问题?


qq_花开花谢_0
浏览 357回答 3
3回答

慕斯709654

使用Try .trim() 可以从 edittext 中获取值。空白来自 edittext ,同时也使用了 "utf-8"String&nbsp;value&nbsp;=&nbsp;URLEncoder.encode(myEditBox.getText.toString().trim(),&nbsp;"utf-8"); String&nbsp;url&nbsp;=&nbsp;"http://xxxxx/xxx/xxx&message=%s"&nbsp;+&nbsp;value;

慕神8447489

String&nbsp;url&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;String.format( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"http://xxxxx/xxx/xxx&message=%s",myEditBox.getText.toString());&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;regEx&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;"\\s";&nbsp; Pattern&nbsp;&nbsp;&nbsp;p&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;Pattern.compile(regEx);&nbsp;&nbsp;&nbsp;&nbsp; url&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;p.matcher(url).replaceAll("").trim();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java