<isol>

@.li:Listing 1: Verwenden von <I>HttpURLConnection<I>

@li:public static Bitmap getBitmapFromServer(String strURL) {
	Bitmap bitmap = null;
	HttpURLConnection httpURLConnection = null;
	try {
		URL url = new URL(strURL);
		httpURLConnection = (HttpURLConnection) url.openConnection();
		int responseCode = httpURLConnection.getResponseCode();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			bitmap = BitmapFactory.decodeStream(httpURLConnection
					.getInputStream());
		}
	} catch (Throwable tr) { // MalformedURLException, IOException
		Log.e(TAG, "Fehler beim Zugriff auf " + strURL, tr);
	} finally {
		if (httpURLConnection != null) {
			httpURLConnection.disconnect();
		}
	}
	return bitmap;
}

@.li:Listing 2: Lesen eines Byte-Stroms

@li:public static byte[] getByteArrayFromServer(String strURL) {
	byte[] bytes = null;
	HttpURLConnection httpURLConnection = null;
	try {
		URL url = new URL(strURL);
		httpURLConnection = (HttpURLConnection) url.openConnection();
		int responseCode = httpURLConnection.getResponseCode();
		Log.d(TAG, "Content-Length: " + httpURLConnection.getContentLength());
		if (responseCode == HttpURLConnection.HTTP_OK) {
			InputStream in = httpURLConnection.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
			int i;
			while ((i = in.read()) != -1) {
				out.write(i);
			}
			bytes = out.toByteArray();
		}
	} catch (Throwable tr) { // MalformedURLException, IOException
		Log.e(TAG, "Fehler beim Zugriff auf " + strURL, tr);
	} finally {
		if (httpURLConnection != null) {
			httpURLConnection.disconnect();
		}
	}
	return bytes;
}

@.li:Listing 3: Ermitteln des passenden Zeichensatzes

@li:public static String getStringFromServer(String strURL) {
	StringBuilder sb = new StringBuilder();
	HttpURLConnection httpURLConnection = null;
	try {
		URL url = new URL(strURL);
		httpURLConnection = (HttpURLConnection) url.openConnection();
		int responseCode = httpURLConnection.getResponseCode();
		String connectionType = httpURLConnection.getContentType();
		Log.d(TAG, "Content-Type: " + connectionType);
		String charSet = "ISO-8859-1";
		if (connectionType != null) {
			// compile sollte aus Effizienzgrnden als Konstante ausgelagert
			// werden
			Pattern p = Pattern.compile(".*charset\\s*=\\s*(.*)$");
			Matcher m = p.matcher(connectionType);
			if (m.matches()) {
				charSet = m.group(1);
			}
		}
		if (responseCode == HttpURLConnection.HTTP_OK) {
			InputStreamReader inputStreamReader = new InputStreamReader(
					httpURLConnection.getInputStream(), charSet);
			BufferedReader bufferedReader = new BufferedReader(
					inputStreamReader);
			int i;
			while ((i = bufferedReader.read()) != -1) {
				sb.append((char) i);
			}
			try {
				bufferedReader.close();
			} catch (IOException e) {
				// ein Fehler beim Schlieen wird bewusst ignoriert
			}
		}
	} catch (Throwable tr) { // MalformedURLException, IOException,
								// NullPointerException,
								// UnsupportedEncodingException
		Log.e(TAG, "Fehler beim Zugriff auf " + strURL, tr);
	} finally {
		if (httpURLConnection != null) {
			httpURLConnection.disconnect();
		}
	}
	return sb.toString();
}

@.li:Listing 4: Prfen der Netzwerkverbindung

@li:private boolean istNetzwerkVerfuegbar() {
	ConnectivityManager mgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo info = mgr.getActiveNetworkInfo();
	if (info != null && info.isConnected()) {
		return true;
	} else {
		return false;
	}
}

@.li:Listing 5: Verwenden von <I>getSystemService<I>

@li:final DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(imageUrl));
downloadId = dm.enqueue(request);

!!!Listing6

@.li:Start der Download-Oberflche

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);

@.li:Listing 7: Status des Downloads

@li:@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();
	if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
		long id = intent.getLongExtra(
				DownloadManager.EXTRA_DOWNLOAD_ID, 0);
		if (downloadId == id) {
			int buttonText = R.string.herunterladen;
			m = Modus.NOT_DOWNLOADING;
			Query query = new Query();
			query.setFilterById(downloadId);
			Cursor c = dm.query(query);
			if (c.moveToFirst()) {
				int columnIndex = c
						.getColumnIndex(DownloadManager.COLUMN_STATUS);
				if (DownloadManager.STATUS_SUCCESSFUL == c
						.getInt(columnIndex)) {
					m = Modus.FINISHED;
					buttonText = R.string.downloads;
				}
			}
			c.close();
			button.setText(buttonText);
		}
	}
}

@.li:Listing 8: Krzen einer URL

@li:httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
byte[] data = json.getBytes();
httpURLConnection.setRequestProperty("Content-Type",
		"application/json; charset="
				+ Charset.defaultCharset().name());
if (key != null) {
	httpURLConnection.setRequestProperty("apikey", key);
}
httpURLConnection.setFixedLengthStreamingMode(data.length);

@.li:Listing 9:Asynchroner Aufruf

@li:@Override
public void onClick(View v) {
	String url = eingabe.getText().toString();
	String json = "{\"longUrl\": \"" + url + "\"}";
	new AsyncTask<String, Void, String>() {

		@Override
		protected String doInBackground(String... params) {
			String json = kuerzeURL(params[0]);
			return json;
		}

		@Override
		protected void onPostExecute(String result) {
			ausgabe.setText(result);
		}

	}.execute(json);
}