Fix crash

This commit is contained in:
huanghongxun 2019-12-14 12:14:08 +08:00
parent 6e79d287f3
commit 61abed04f3
3 changed files with 18 additions and 22 deletions

View File

@ -237,7 +237,7 @@ public class FileDownloadTask extends Task<Void> {
} }
int contentLength = con.getContentLength(); int contentLength = con.getContentLength();
if (contentLength < 1) if (contentLength < 0)
throw new IOException("The content length is invalid."); throw new IOException("The content length is invalid.");
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile())) if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile()))

View File

@ -89,7 +89,7 @@ public final class GetTask extends Task<String> {
updateProgress(0); updateProgress(0);
HttpURLConnection conn = NetworkUtils.createConnection(url); HttpURLConnection conn = NetworkUtils.createConnection(url);
if (checkETag) repository.injectConnection(conn); if (checkETag) repository.injectConnection(conn);
conn.connect(); conn = NetworkUtils.resolveConnection(conn);
if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Handle cache // Handle cache

View File

@ -36,26 +36,22 @@ public final class NetworkUtils {
} }
public static String withQuery(String baseUrl, Map<String, String> params) { public static String withQuery(String baseUrl, Map<String, String> params) {
try { StringBuilder sb = new StringBuilder(baseUrl);
StringBuilder sb = new StringBuilder(baseUrl); boolean first = true;
boolean first = true; for (Entry<String, String> param : params.entrySet()) {
for (Entry<String, String> param : params.entrySet()) { if (param.getValue() == null)
if (param.getValue() == null) continue;
continue; if (first) {
if (first) { sb.append('?');
sb.append('?'); first = false;
first = false; } else {
} else { sb.append('&');
sb.append('&');
}
sb.append(URLEncoder.encode(param.getKey(), "UTF-8"));
sb.append('=');
sb.append(URLEncoder.encode(param.getValue(), "UTF-8"));
} }
return sb.toString(); sb.append(encodeURL(param.getKey()));
} catch (IOException e) { sb.append('=');
throw new UncheckedIOException(e); sb.append(encodeURL(param.getValue()));
} }
return sb.toString();
} }
public static HttpURLConnection createConnection(URL url) throws IOException { public static HttpURLConnection createConnection(URL url) throws IOException {
@ -198,8 +194,8 @@ public final class NetworkUtils {
public static URL toURL(String str) { public static URL toURL(String str) {
try { try {
return new URL(URLEncoder.encode(str, "UTF-8")); return new URL(str);
} catch (MalformedURLException | UnsupportedEncodingException e) { } catch (MalformedURLException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }