Getting string from json object not working

Viewed 321

I am using gson library to get the json from an http request. Everything works fine, except the part where I compare the string I received from the request. Even if the string are exactly the same, for some reason string.equals method fails. And as output it prints always different dates.

What is the reason for this behaviour? What am I missing here?

 BufferedReader br;
    try{
        String url = "http://date.jsontest.com/";
        URL request_url = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)request_url.openConnection();
        conn.setRequestMethod("GET");

        if (200 == conn.getResponseCode()){
            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String jsonLine = "";
            String line ;
            while ((line = br.readLine()) != null) {
                jsonLine += line;
            }

            JsonElement jelement = new JsonParser().parse(jsonLine);
            JsonObject  jobject = jelement.getAsJsonObject();

            try{
                String result = jobject.get("date").toString();
                System.out.println("res: " + result);

                if(result.equals("05-29-2017"))
                    System.out.println("Same date");
                else
                    System.out.println("different date");
            }
            catch (NullPointerException ex){ }
        }
    }
    catch (JsonSyntaxException ex){} 
    catch (IOException ex) {}
1 Answers
Related