Search code examples
javajspurlutf8-decode

How to decode String?


Hi i am trying to fetch an url by which i am getting a string i am trying to decode that string but whole string is not decoding how can i decode whole string decode

Here is my code

geturl.jsp

<%

    URL url;

    try {
        // get URL content

        String a = "http://122.160.81.37:8080/mandim/MarketWise?m=agra";
        url = new URL(a);
        URLConnection conn = url.openConnection();
        // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            String str = new String(inputLine.getBytes(), "utf-8");
            out.println(str);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
%>

i am getting following output

धान~1325|चावल~2050|ज�?वर~920|जौ~810|मकई~1280|गेहू�?~1420|जो~1050|बेजर~-|जय~800|उड़द~3600|मूंग~4400|चाना~3400|मटर~2700|अरहर~4100|मसूर~2000|लोबिया(बीज)~-|सोयाबीन~-|ढे�?चा(बीज)~-

here i am getting some ? symbol

Desired output

धान~1325|चावल~2050|ज्वर~920|जौ~810|मकई~1280|गेहूँ~1420|जो~1050|बेजर~-|जय~800|उड़द~3600|मूंग~4400|चाना~3400|मटर~2700|अरहर~4100|मसूर~2000|लोबिया(बीज)~-|सोयाबीन~-|ढेँचा(बीज)~-

How can i get my desired output?

Thanks in advance


Solution

  • Specify the encoding when you create he reader:

    new InputStreamReader(conn.getInputStream(), "UTF-8")); 
    

    Don't try to do any other conversions.