Solved

How to encode URL which contains accented character in JAVA

Posted on 2011-02-15
6
1,800 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 83
even odd program using while loop 3 40
Java string replace 11 47
Crystal Reports Licensing Questions 4 10
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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