How to encode URL which contains accented character in JAVA


I have written a java code.This code basically take city name,state name and country name from a xml file and will make a call  to the following webservice :  "<cityname>&state=<statename>&country=<countryname>&flags=X&gflags=A"..
When I run the code it says  "invalid uri"
The city name,state name or country name contais accented characters.Basically they contain french,spanish,german characters.I can say european characters.
Examples:city=Pobra Do Caramiñal (A),Santa Lucía De Tirajana
                state name=CORUñA, A

I tried doing replace each character but it is very time consuming as I have 2000 data.
Please respond me soon as I have already cross the deadline in my project.

I have given my code snippet below:

public String getWoeidfromWS() throws IOException{
         String woe_id=null;
         String queryParam =null;
         String request=null;        
         String uri="";  
            if(Name != null && (Name.length()!=0)){
                     queryParam= "&" + "city=" + Name ;
            if(stateName != null&& (stateName.length()!=0)){
                     queryParam=queryParam+ "&" + "state=" + stateName ;
            if(countryName != null&& (countryName.length()!=0)){
                    queryParam=queryParam + "&" +"country=" +countryName;
            request = uri+ queryParam + "&flags=X" + "&gflags=A";
                System.out.println("Origional Request is:" +request);
                //request = request.replace(" ", "%20");
                /*request = request.replace("ï", "%EF");
                request = request.replace("é", "%E8");
                request = request.replace("á", "%E1");
                request = request.replace("É", "%C9");
                request = request.replace("Î", "%CE");
                request = request.replace("ñ", "%F1");
                request = request.replace("ó", "%F3");
                request = request.replace("í", "%ED");
                request = request.replace("ö", "%F6");*/                
                GetMethod method = new GetMethod(request);
                InputStream response = method.getResponseBodyAsStream();
                File file = new File("city_output.xml");
                FileOutputStream stream = new FileOutputStream(file);
                BufferedOutputStream out = new BufferedOutputStream(stream);
                int length = 0;
                byte[] buf = new byte[1024];
                try {
                    System.out.println("starting jdom for city_out.xml");
                } catch (JDOMException e) {
              return woe_id;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Have you tried using URLencoder.encode(cityname, "UTF-8"); for each variable?
request = URLEncoder.encode(uri+ queryParam + "&flags=X" + "&gflags=A", "UTF-8");

Open in new window

In my experience, it is better to encode each parameter without &country=
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

I think you're probably correct.  We don't want to encode the & itself.
Mick BarryJava DeveloperCommented:
yes you should be encoding only the parameter value

            if(Name != null && (Name.length()!=0)){
                     queryParam= "&" + "city=" + URLencoder.encode(Name, "UTF8") ;
            if(stateName != null&& (stateName.length()!=0)){
                     queryParam=queryParam+ "&" + "state=" + URLencoder.encode(stateName, "UTF8") ;
            if(countryName != null&& (countryName.length()!=0)){
                    queryParam=queryParam + "&" +"country=" +URLencoder.encode(countryName, "UTF8");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mansi21Author Commented:
Hey thanks a lot.I am really very happy today as It worked and I was able to submit my task.Once again THANKS A LOT!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.