Solved

How to encode URL which contains accented character in JAVA

Posted on 2011-02-15
6
1,825 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 55
ejb wildfly example 2 27
Tagging and Merging on Branch 1 30
Setup GlassFish 4 22
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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