3

Team,

I have come across the code like below,

private static String getResponse (HttpURLConnection connection) throws Exception { String responseString = null; int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); Log.debug("%s - Response code is \"%s\" with message \"%s\"", methodName, responseCode, responseMessage); String line = null; if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader bufferedReader = null; try { InputStream inputStream = connection.getInputStream(); if (inputStream != null) { bufferedReader = Util.bufferedReader( inputStream, Util.Encod.UTF8); StringBuilder response = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { response.append(line); response.append(Util.getNewLine()); } responseString = response.toString(); } } finally { if (bufferedReader != null) { bufferedReader.close(); } } Log.signature.debug( "%s - Received following JSON response : %s", methodName, responseString); } return responseString; } 

Here they have already received the response like below right?

String responseMessage = connection.getResponseMessage() 

Then why they are again using connection.getInputStream()

Is there any difference?

If possible could you please also explain some example OR when to use the below than the above getResponseMessage() / getInputStream()

Class URLConnection public Object getContent() throws IOException public Object getContent(Class[] classes) throws IOException 

1 Answer 1

3

getResponseMessage() is used for getting the message for the connection like HTTP_NOT_FOUND

HTTP Status-Code 404: Not Found. 

For getting the actual data you have to through getInputStream() please find below details:

public InputStream getInputStream() throws IOException 

Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

Returns: an input stream that reads from this open connection.

Throws: IOException - if an I/O error occurs while creating the input stream. UnknownServiceException - if the protocol does not support input.

Please refer following link for more details : http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getInputStream()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Could you please add some answer on how to use getContent() API as well ?
getContent() Returns an object representing the content of the resource this URLConnection is connected to. It returns the output of the content handler. Since the content handler can return almost anything, the return type of getContent() is Object. Hence we must cast it into some other data type that we can work with. For example, if we expect a String, we'll cast the result of getContent() to a String

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.