From bcf2cb04ce7f2f69be5df8416654b366ae50b2b0 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Wed, 18 Jan 2012 01:46:40 +0100 Subject: [PATCH] Last change using HttpClient, I keep reading the API :( --- .../Test3/src/de/android/test3/NextActivity.java | 68 +++++++++++----------- .../Test3/src/de/android/test3/Test3Activity.java | 3 + 2 files changed, 38 insertions(+), 33 deletions(-) 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) { -- 2.1.4