0

I'm using HttpUrlConnection to consume REST services. When I do GET, like the one I show below, I don't want to get the information that is returned char by char. I want to get the result in the format that it is returned. Here, for example, I want to get a boolean, and not something like System.out.print((char) ch);. How can I receive it?

I know that I can parse a String into a Boolean type, but if I receive another data type?

public class SensorGetDoorStatus {

public static void main(String[] args) throws Exception
{
    HttpURLConnection urlConnection = null;

try {
    String webPage = "http://localhost:8080/LULServices/webresources/services.sensors/doorstatus";
            String name = "xxxx";
    String password = "xxxx";

            Authenticator myAuth = new Authenticator() 
            {
              final String USERNAME = "xxxx";
              final String PASSWORD = "xxxxx";

              @Override
              protected PasswordAuthentication getPasswordAuthentication()
              {
                return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
              }
            };

            Authenticator.setDefault(myAuth);

    String authString = name + ":" + password;
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);

    URL urlToRequest = new URL(webPage);
    urlConnection = (HttpURLConnection) urlToRequest.openConnection();

    urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
    System.out.println("Authorization : Basic " + authStringEnc);

    urlConnection.setRequestMethod("GET");
    urlConnection.setRequestProperty("Accept", "application/json");                
    urlConnection.setReadTimeout(15*1000);
    urlConnection.connect();

    InputStream inStrm = urlConnection.getInputStream();

    int ch;
    while (((ch = inStrm.read()) != -1))
         System.out.print((char) ch);
    inStrm.close(); 


} catch (MalformedURLException e) {
        e.printStackTrace();
} catch (IOException e) {
        System.out.println("Failure processing URL");
        e.printStackTrace();
} catch (Exception e) {
        e.printStackTrace();
    }

    finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}
 }

}

javanna
  • 59,145
  • 14
  • 144
  • 125
user2144555
  • 1,315
  • 11
  • 34
  • 55

2 Answers2

1

I'd play around with BufferedReader: it allows to read an InputStream line by line, and you might be able to parse your data that way.

If your data is in a predefined format like XML or JSON, you'd better use a library to parse your response data, though.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
1

You might want to look at using DataInputStream. You can use the method readBoolean method of this class to read a boolean.

The DataInputStream and DataOutputStream also provides you with methods to write and read specific data types for example int, float, long etc

You should have written writeBoolean of class DataOutputStream on the other end from where you are sending data.

The code will look like follwoing:

 InputStream inStrm = urlConnection.getInputStream();
 DataInputStream doi = new DataInputStream(inStrm);
 boolean bol = doi.readBoolean();

  doi.close();
  inStrm.close(); 
user_CC
  • 4,686
  • 3
  • 20
  • 15