Solved

How to encode URL which contains accented character in JAVA

Posted on 2011-02-15
6
1,779 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 97
Java Message handling in Service Layer 3 37
advertisement module in core php 4 89
Java DateChooser? 2 11
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now