mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -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 {
|
private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
|
||||||
Log.i(TAG, "Downloading " + uri);
|
Log.i(TAG, "Downloading " + uri);
|
||||||
URL url = new URL(uri);
|
URL url = new URL(uri);
|
||||||
URLConnection conn = url.openConnection();
|
HttpURLConnection connection = safelyOpenConnection(url);
|
||||||
if (!(conn instanceof HttpURLConnection)) {
|
|
||||||
throw new IOException();
|
|
||||||
}
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) conn;
|
|
||||||
connection.setRequestProperty("Accept", contentTypes);
|
connection.setRequestProperty("Accept", contentTypes);
|
||||||
connection.setRequestProperty("Accept-Charset", "utf-8,*");
|
connection.setRequestProperty("Accept-Charset", "utf-8,*");
|
||||||
connection.setRequestProperty("User-Agent", "ZXing (Android)");
|
connection.setRequestProperty("User-Agent", "ZXing (Android)");
|
||||||
|
@ -151,12 +147,7 @@ public final class HttpHelper {
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
URL url = uri.toURL();
|
URL url = uri.toURL();
|
||||||
|
HttpURLConnection connection = safelyOpenConnection(url);
|
||||||
URLConnection conn = url.openConnection();
|
|
||||||
if (!(conn instanceof HttpURLConnection)) {
|
|
||||||
throw new IOException();
|
|
||||||
}
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) conn;
|
|
||||||
connection.setInstanceFollowRedirects(false);
|
connection.setInstanceFollowRedirects(false);
|
||||||
connection.setDoInput(false);
|
connection.setDoInput(false);
|
||||||
connection.setRequestMethod("HEAD");
|
connection.setRequestMethod("HEAD");
|
||||||
|
@ -183,6 +174,21 @@ public final class HttpHelper {
|
||||||
connection.disconnect();
|
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 {
|
private static int safelyConnect(String uri, HttpURLConnection connection) throws IOException {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue