From: gumartinm Date: Wed, 18 Jan 2012 00:46:40 +0000 (+0100) Subject: Last change using HttpClient, I keep reading the API :( X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=bcf2cb04ce7f2f69be5df8416654b366ae50b2b0;p=JavaForFun Last change using HttpClient, I keep reading the API :( --- diff --git a/Android/Testing/Test3/src/de/android/test3/NextActivity.java b/Android/Testing/Test3/src/de/android/test3/NextActivity.java index bebe5e6..04c7d8d 100644 --- a/Android/Testing/Test3/src/de/android/test3/NextActivity.java +++ b/Android/Testing/Test3/src/de/android/test3/NextActivity.java @@ -192,39 +192,41 @@ public class NextActivity extends Activity { public StringBuilder sortResponse(HttpResponse httpResponse) throws UnsupportedEncodingException, IllegalStateException, IOException { StringBuilder builder = null; - switch (httpResponse.getStatusLine().getStatusCode()) { - case HttpStatus.SC_OK: - //OK - HttpEntity entity = httpResponse.getEntity(); - if (entity != null) { - //outside try/catch block. - //In case of exception it is thrown, otherwise instream will not be null and we get into the try/catch block. - InputStreamReader instream = new InputStreamReader(entity.getContent(), entity.getContentEncoding().getValue()); - try { - BufferedReader reader = new BufferedReader(instream); - builder = new StringBuilder(); - String currentLine = null; - currentLine = reader.readLine(); - while (currentLine != null) { - builder.append(currentLine).append("\n"); - currentLine = reader.readLine(); - } - } finally { - //instream will never be null in case of reaching this code, cause it was initialized outside try/catch block. - //In case of exception here, it is thrown to the superior layer. - instream.close(); - } - } - break; - case HttpStatus.SC_UNAUTHORIZED: - //ERROR IN USERNAME OR PASSWORD - break; - case HttpStatus.SC_BAD_REQUEST: - //WHAT THE HECK ARE YOU DOING? - break; - default: - Log.e(TAG, "Error while retrieving the HTTP status line."); - break; + if (httpResponse != null) { + switch (httpResponse.getStatusLine().getStatusCode()) { + case HttpStatus.SC_OK: + //OK + HttpEntity entity = httpResponse.getEntity(); + if (entity != null) { + //outside try/catch block. + //In case of exception it is thrown, otherwise instream will not be null and we get into the try/catch block. + InputStreamReader instream = new InputStreamReader(entity.getContent(), entity.getContentEncoding().getValue()); + try { + BufferedReader reader = new BufferedReader(instream); + builder = new StringBuilder(); + String currentLine = null; + currentLine = reader.readLine(); + while (currentLine != null) { + builder.append(currentLine).append("\n"); + currentLine = reader.readLine(); + } + } finally { + //instream will never be null in case of reaching this code, cause it was initialized outside try/catch block. + //In case of exception here, it is thrown to the superior layer. + instream.close(); + } + } + break; + case HttpStatus.SC_UNAUTHORIZED: + //ERROR IN USERNAME OR PASSWORD + break; + case HttpStatus.SC_BAD_REQUEST: + //WHAT THE HECK ARE YOU DOING? + break; + default: + Log.e(TAG, "Error while retrieving the HTTP status line."); + break; + } } return builder; diff --git a/Android/Testing/Test3/src/de/android/test3/Test3Activity.java b/Android/Testing/Test3/src/de/android/test3/Test3Activity.java index 046a62e..8f2ffb1 100644 --- a/Android/Testing/Test3/src/de/android/test3/Test3Activity.java +++ b/Android/Testing/Test3/src/de/android/test3/Test3Activity.java @@ -108,6 +108,9 @@ public class Test3Activity extends Activity { break; } } + else { + Log.e(TAG, "No response? This should never have happened."); + } } public void onClickCancel(View v) {