Question

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

Was it helpful?

Solution 2

Specify the encoding when you create he reader:

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

Don't try to do any other conversions.

OTHER TIPS

If you can assume that the encoding is "UTF-8", then use @Joni's solution.

But a more technically correct solution is to:

  1. use conn.getHeaderField("content-type") to extract the response mediatype,
  2. extract the charset parameter (if any), and
  3. use that as the charset for decoding the response input stream.

At the same time you should probably check that the mediatype is what you are expecting and check the response status code.

References:

You are able to see many of the characters with utf-8 encoding, except few. So make sure you ,in your system, proper fonts have been installed that supports all these.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top