diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-01-18 00:02:19 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-01-18 00:02:19 +0100 |
| commit | e8d9746d808daefdafda1468a66fbf0cbb3d7ca5 (patch) | |
| tree | c29fdaddf13b4e0edd4bec413f9e4f6881924683 /main/src/cgeo/geocaching/network | |
| parent | 0735516b8c2b49fc7f5e1d4f7005fea412f7cfa7 (diff) | |
| download | cgeo-e8d9746d808daefdafda1468a66fbf0cbb3d7ca5.zip cgeo-e8d9746d808daefdafda1468a66fbf0cbb3d7ca5.tar.gz cgeo-e8d9746d808daefdafda1468a66fbf0cbb3d7ca5.tar.bz2 | |
Ensure that we do not generate a NPE while reading response stream
Diffstat (limited to 'main/src/cgeo/geocaching/network')
| -rw-r--r-- | main/src/cgeo/geocaching/network/Network.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index b737fd2..ffd58c3 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -47,6 +47,7 @@ import android.net.Uri; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; @@ -425,6 +426,30 @@ public abstract class Network { return null; } + /** + * Get the input stream corresponding to a HTTP response if it exists. + * + * @param response a HTTP response, which can be null + * @return the input stream if the HTTP request is successful, <code>null</code> otherwise + */ + @Nullable + public static InputStream getResponseStream(@Nullable final HttpResponse response) { + if (!isSuccess(response)) { + return null; + } + assert(response != null); + final HttpEntity entity = response.getEntity(); + if (entity == null) { + return null; + } + try { + return entity.getContent(); + } catch (final IOException e) { + Log.e("Network.getResponseStream", e); + return null; + } + } + @Nullable private static String getResponseDataNoError(final HttpResponse response, boolean replaceWhitespace) { try { |
