Solved

How to encode URL which contains accented character in JAVA

Posted on 2011-02-15
6
1,862 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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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

737 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