2

I am sending request to server by using this following url format
http://api.xyz.com/JSonService.asmx/Category_Name=food-and-drink.

But my problem is i am not able to send the request as hyphen present in food-and-drink,but in my system browser it is working fine..

Thanks in advance.

Mr Nice
  • 524
  • 8
  • 24

1 Answers1

1

You need to use URLEncoder

String queryStr = URLEncoder.encode("food-and-drink", "utf-8");
String urlStr = "http://api.xyz.com/JSonService.asmx/Category_Name=" + queryStr;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • 1
    Please notice that this example in not entirely correct because you don't have to encode the entire URL but just the relevant part like the query string. If you encode the whole URL, also "http://" will be encode as "http%3A%2F%2F" and it will likely cause a java.net.MalformedURLException. – fasteque Jan 17 '14 at 08:50