Datei Download: failed to connect to...: connect failed: ECONNREFUSED

  • 0 Antworten
  • Letztes Antwortdatum
myeta

myeta

Erfahrenes Mitglied
59
Servus Leute,

ich möchte ein Bild von einem Server herunterladen und auf der SD Karte speichern.
Leider bekomme ich die Excpetion, dass wohl keine Verbindung zu dem Server aufgenommen werden konnte.

Der Download findet in einem Nicht-UI-Thread statt.

Hier die Download-Klasse:
PHP:
package de.mammuth.calendar.word;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.Environment;
import android.os.Message;

/**
 * Downloads a file in a thread. Will send messages to the MainActivity activity
 * to update the progress bar.
 */
public class DownloaderThread extends Thread {
	// constants
	private static final int DOWNLOAD_BUFFER_SIZE = 4096;

	// instance variables
	private MainActivity parentActivity;
	private String downloadUrl;

	/**
	 * Instantiates a new DownloaderThread object.
	 * 
	 * @param parentActivity
	 *            Reference to MainActivity activity.
	 * @param inUrl
	 *            String representing the URL of the file to be downloaded.
	 */
	public DownloaderThread(MainActivity inParentActivity, String inUrl) {
		downloadUrl = "";
		if (inUrl != null) {
			downloadUrl = inUrl;
		}
		parentActivity = inParentActivity;
	}

	/**
	 * Connects to the URL of the file, begins the download, and notifies the
	 * MainActivity activity of changes in state. Writes the file to the root of
	 * the SD card.
	 */
	@Override
	public void run() {
		URL url;
		URLConnection conn;
		int fileSize, lastSlash;
		String fileName;
		BufferedInputStream inStream;
		BufferedOutputStream outStream;
		File outFile;
		FileOutputStream fileStream;
		Message msg;

		try {
			url = new URL(downloadUrl);
			conn = url.openConnection();
			conn.setUseCaches(false);
			fileSize = conn.getContentLength();

			// get the filename
			lastSlash = url.toString().lastIndexOf('/');
			fileName = "file.bin";
			if (lastSlash >= 0) {
				fileName = url.toString().substring(lastSlash + 1);
			}
			if (fileName.equals("")) {
				fileName = "file.bin";
			}

			// notify download start
			int fileSizeInKB = fileSize / 1024;

			// start download
			inStream = new BufferedInputStream(conn.getInputStream());
			outFile = new File(Environment.getExternalStorageDirectory()
					+ "/1ATest/" + fileName);
			fileStream = new FileOutputStream(outFile);
			outStream = new BufferedOutputStream(fileStream,
					DOWNLOAD_BUFFER_SIZE);
			byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
			int bytesRead = 0, totalRead = 0;
			while (!isInterrupted()
					&& (bytesRead = inStream.read(data, 0, data.length)) >= 0) {
				outStream.write(data, 0, bytesRead);

				// update progress bar
				totalRead += bytesRead;
				int totalReadInKB = totalRead / 1024;

			}

			outStream.close();
			fileStream.close();
			inStream.close();

			if (isInterrupted()) {
				// the download was canceled, so let's delete the partially
				// downloaded file
				outFile.delete();
			} else {
				// notify completion

			}
		} catch (MalformedURLException e) {
			String errMsg = "error";
			System.out.println("häng im catch1");

		} catch (FileNotFoundException e) {
			System.out.println("häng im catch2");
		} catch (Exception e) {
			System.out.println("getMessage(): " + e.getMessage());
//Das ist die Exception
		}
	}

}
Kann mir jemand auf die Sprünge helfen, wo das Problem liegt?
Herzlichen Dank.

Greets,
Max
 

Ähnliche Themen

AnnaBauer21
Antworten
6
Aufrufe
946
AnnaBauer21
AnnaBauer21
AnnaBauer21
Antworten
0
Aufrufe
476
AnnaBauer21
AnnaBauer21
D
Antworten
8
Aufrufe
753
jogimuc
J
S
Antworten
7
Aufrufe
1.142
Silvasurf
S
Zurück
Oben Unten