HttpURLConnection unter Android v4.x stürzt ab

Gibbsnich

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?

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 ;-)
 
Hallo

Welche Exception bekommst du denn genau?

Was mir spontan einfällt: Seit Android 3 dürfen HTTP-Verbindungen nicht mehr im Main-Thread ausgeführt werden, sondern müssen in einen eigenen Thread ausgelagert werden. Dann würdest du eine "NetworkOnMainThreadException" bekommen.

Da Andorid 3 ausschließlich auf Tablets verwendet wird und wenn du deine App noch nie auf einem Tablet debuggt hast, könnte dies durchaus wahrscheinlich sein.
 
  • Danke
Reaktionen: Gibbsnich
Hallo Son Goku,


Son Goku schrieb:
Welche Exception bekommst du denn genau?

da es sich um ein NetworkOnMainThreadException handelt, liegst du vollkommen richtig ;-)

Kannst du mir jetzt noch verraten, warum das Ganze reibungslos funktioniert hat, bis ich in der Manifest-Datei die minSdkVersion von 3 auf 5 angehoben habe?
Hätte da die Exception nicht auch schon kommen müssen?

Du hast mir auf jeden Fall sehr geholfen.
Vielen Dank!!
 
StefMa schrieb:
Kann nicht sein.
Die Meldung muss genau dann kommen, als du die targetSdk-Version auf >= 11 gestuft hast.

Stimmt, das hatte ich auch geändert :ohmy:
 

Ähnliche Themen

S
Antworten
8
Aufrufe
511
swa00
swa00
B
Antworten
4
Aufrufe
487
bb321
B
L
Antworten
15
Aufrufe
908
jogimuc
J
Zurück
Oben Unten