从字符串中删除空格以进行http连接

时间:2012-11-01 08:17:46

标签: java android

我正在尝试发送查询网址

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。那可能是white space。如何处理这个问题?

最好的问候

3 个答案:

答案 0 :(得分:5)

您需要对您的网址进行编码。

String query = URLEncoder.encode(myEditBox.getText.toString(), "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

答案 1 :(得分:0)

你能试试吗

 httpclient.setRequestProperty("Accept-Charset","UTF-8");
 url="http://xxxxx/xxx/xxx&message="+URLEncoder.encode(myEditBox.getText.toString(), "UTF-8");

答案 2 :(得分:0)

从edittext获取值时尝试.trim()。 可能是来自edittext的空格,也可以使用&#34; utf-8&#34;。

见下面的代码。

String value = URLEncoder.encode(myEditBox.getText.toString().trim(), "utf-8");
String url = "http://xxxxx/xxx/xxx&message=%s" + value;