Gibbsnich
Ambitioniertes Mitglied
- 0
Hallo,
ich lade in meiner App seit langem kleine Images vom Server herunter und nutze dabei nachfolgenden Code, den ich im Internet gefunden habe.
Das ging alles wunderbar, aber unter Android V4.x wird bei httpVerbindung.getResponseCode() plötzlich immer eine Exception ausgelöst und daher wird dei Antwort des Servers nicht mehr gelesen.
Was hat sich denn geändert, bzw. was muss ich ändern, damit das wieder funktioniert?
Danke für's bis hierhin Lesen und womöglich auch für eine hilfreiche Antwort ;-)
ich lade in meiner App seit langem kleine Images vom Server herunter und nutze dabei nachfolgenden Code, den ich im Internet gefunden habe.
Das ging alles wunderbar, aber unter Android V4.x wird bei httpVerbindung.getResponseCode() plötzlich immer eine Exception ausgelöst und daher wird dei Antwort des Servers nicht mehr gelesen.
Was hat sich denn geändert, bzw. was muss ich ändern, damit das wieder funktioniert?
Code:
protected static byte[] downloadImage(Context ctx, String strURL) {
HttpURLConnection httpVerbindung = null;
OutputStream out = null;
byte[] respData = null;
try {
String myURL = "MeinServer:Port/img=" + URLEncoder.encode(strURL, "utf-8");
URL url = new URL(myURL);
httpVerbindung = (HttpURLConnection) url.openConnection();
httpVerbindung.setRequestMethod("GET");
httpVerbindung.setDoOutput(false);
httpVerbindung.setDoInput(true);
httpVerbindung.setConnectTimeout(5000);
httpVerbindung.setReadTimeout(5000);
httpVerbindung.connect();
} catch (MalformedURLException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exMalformedError, e.toString())));
} catch (SocketTimeoutException e) {
Protokoll.logError("downloadImage", BIS_Common.bisActivity.getString(R.string.exTimeoutError));
} catch (UnsupportedEncodingException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exEncodingError, e.toString())));
} catch (IOException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exIOError, e.toString())));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exIOCloseError, e.toString())));
}
}
}
if (httpVerbindung != null) {
InputStream in = null;
try {
//==> Hier kommt die Exption:
int responseCode = httpVerbindung.getResponseCode();
//<==
if (responseCode == HttpURLConnection.HTTP_OK) {
getCookieData(httpVerbindung);
in = httpVerbindung.getInputStream();
int length = httpVerbindung.getContentLength();
if (length > 0) {
respData = new byte[length];
int total = 0;
while (total < length) {
total += in.read(respData, total, length - total);
}
}
else {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
bytestream.write(ch);
}
respData = bytestream.toByteArray();
bytestream.close();
}
in.close();
}
} catch (IOException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exIOError, e.toString())));
}
finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
Protokoll.logError("downloadImage", String.format(BIS_Common.bisActivity.getString(R.string.exInCloseError, e.toString())));
}
}
if (httpVerbindung != null) {
httpVerbindung.disconnect();
}
}
}
if (respData != null) {
return respData;
} else return null;
}
Danke für's bis hierhin Lesen und womöglich auch für eine hilfreiche Antwort ;-)