?
Solved

How to encode URL which contains accented character in JAVA

Posted on 2011-02-15
6
Medium Priority
?
1,920 Views
Last Modified: 2012-05-11
Hi,

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 :  "http://flb.intl.yahoo.com/FindLocation?appid=travel.off&city=<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="http://flb.intl.yahoo.com/FindLocation?appid=travel.off";  
             
            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);
                client.executeMethod(method);
               
                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];
                while((length=response.read(buf))>0)
                {
                    out.write(buf,0,length);
                }
               
                out.flush();
                out.close();
                stream.flush();
                stream.close();
                try {
                    System.out.println("starting jdom for city_out.xml");
                   woe_id=retrieveWoeidUsingJDOM();
                } catch (JDOMException e) {
                    e.printStackTrace();
                }
              return woe_id;
            }
   }
url-encode.txt
0
Comment
Question by:mansi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:__geof__
ID: 34900146
Have you tried using URLencoder.encode(cityname, "UTF-8"); for each variable?
0
 
LVL 7

Expert Comment

by:garypfirstech
ID: 34900219
Try
request = URLEncoder.encode(uri+ queryParam + "&flags=X" + "&gflags=A", "UTF-8");

Open in new window

0
 
LVL 6

Expert Comment

by:__geof__
ID: 34900368
In my experience, it is better to encode each parameter without &country=
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:garypfirstech
ID: 34900891
I think you're probably correct.  We don't want to encode the & itself.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 34901516
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");
                    }
0
 

Author Closing Comment

by:mansi21
ID: 34906309
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!!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question