Muhammad Umar
Muhammad Umar

Reputation: 11782

Removing White spaces from a string for http connection

I am trying to send a query 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);

gives me error, illegal character at query. That's white space probably. How to deal with this issue?

Best Regards

Upvotes: 1

Views: 6063

Answers (3)

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Try .trim() while get value from edittext. May be whitespace come from edittext and also use "utf-8".

see below code.

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

Upvotes: 0

anishsane
anishsane

Reputation: 20980

Can you try

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

Upvotes: 0

Chirag
Chirag

Reputation: 56935

You need to encode your url.

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

Upvotes: 5

Related Questions