mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -08:00
Catch weird openConnection() NPE in Android
git-svn-id: https://zxing.googlecode.com/svn/trunk@2568 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
64e69a3e35
commit
2170a1cb0b
|
@ -90,11 +90,7 @@ public final class HttpHelper {
|
|||
private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
|
||||
Log.i(TAG, "Downloading " + uri);
|
||||
URL url = new URL(uri);
|
||||
URLConnection conn = url.openConnection();
|
||||
if (!(conn instanceof HttpURLConnection)) {
|
||||
throw new IOException();
|
||||
}
|
||||
HttpURLConnection connection = (HttpURLConnection) conn;
|
||||
HttpURLConnection connection = safelyOpenConnection(url);
|
||||
connection.setRequestProperty("Accept", contentTypes);
|
||||
connection.setRequestProperty("Accept-Charset", "utf-8,*");
|
||||
connection.setRequestProperty("User-Agent", "ZXing (Android)");
|
||||
|
@ -151,12 +147,7 @@ public final class HttpHelper {
|
|||
return uri;
|
||||
}
|
||||
URL url = uri.toURL();
|
||||
|
||||
URLConnection conn = url.openConnection();
|
||||
if (!(conn instanceof HttpURLConnection)) {
|
||||
throw new IOException();
|
||||
}
|
||||
HttpURLConnection connection = (HttpURLConnection) conn;
|
||||
HttpURLConnection connection = safelyOpenConnection(url);
|
||||
connection.setInstanceFollowRedirects(false);
|
||||
connection.setDoInput(false);
|
||||
connection.setRequestMethod("HEAD");
|
||||
|
@ -183,6 +174,21 @@ public final class HttpHelper {
|
|||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpURLConnection safelyOpenConnection(URL url) throws IOException {
|
||||
URLConnection conn;
|
||||
try {
|
||||
conn = url.openConnection();
|
||||
} catch (NullPointerException npe) {
|
||||
// Another strange bug in Android?
|
||||
Log.w(TAG, "Bad URI? " + url);
|
||||
throw new IOException(npe.toString());
|
||||
}
|
||||
if (!(conn instanceof HttpURLConnection)) {
|
||||
throw new IOException();
|
||||
}
|
||||
return (HttpURLConnection) conn;
|
||||
}
|
||||
|
||||
private static int safelyConnect(String uri, HttpURLConnection connection) throws IOException {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue