Search code examples
javaurlcharacter-encodingutf8-decode

call url from java with special characters


I have an URL with a special character, which I like to request, but URL is not recognized from JAVA. URL : http://en.wikipedia.org/w/api.php?action=query&prop=langlinks&titles=Iași&redirects=&lllimit=400 ( Special Character is ș from the word Iași )

My Java Code :

String sWikiID="Iași";
String sArticleEncoded = URLEncoder.encode(sWikiID);
String sURL = "http://" + sFromLang + ".wikipedia.org" + "/w/api.php?action=query&prop=langlinks&titles=" + sArticleEncoded + "&redirects=&lllimit=400";
URL url = new URL(sURL);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

Any hints ?


Solution

  • You didn't define what sFromLang is, but check if this works for you:

    public static void main(String[] args) throws IOException {
    
        String sFromLang="<add the value here>";
        String sWikiID="Iași";
        String sArticleEncoded = URLEncoder.encode(sWikiID, "UTF-8");
        String sURL = "http://" + sFromLang + ".wikipedia.org" + "/w/api.php?action=query&prop=langlinks&titles=" + sArticleEncoded + "&redirects=&lllimit=400";
        URL url = new URL(sURL);
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
    }