Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

java - Android: openStream fails on URL object

I am trying to test-download a file on android but it fails. In the code, I am first opening a stream from a URL object:

import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;



public class Downloader {
    public static void downloadFile(String address, String outputFileName) throws IOException {
        Log.e("JAVA_FILE_DOWNLOADER", "0");
        URL url = new URL(address);
        Log.e("JAVA_FILE_DOWNLOADER", "1");
        try
        {
            Log.e("JAVA_FILE_DOWNLOADER", "1.5");
            InputStream in = url.openStream();
            Log.e("JAVA_FILE_DOWNLOADER", "2");
            ReadableByteChannel rbc = Channels.newChannel(in);
            Log.e("JAVA_FILE_DOWNLOADER", "3");
            FileOutputStream fos = new FileOutputStream(outputFileName);
            Log.e("JAVA_FILE_DOWNLOADER", "4");
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            Log.e("JAVA_FILE_DOWNLOADER", "5");
        }catch (IOException e) {
            Log.e("JAVA_FILE_DOWNLOADER", "exception catched 1", e);
            e.printStackTrace();
        }
    }
}

Then I have the code running in a thread:

        new Thread(new Runnable() {
            public void run() {
                try {
                    Downloader.downloadFile("http://ipv4.download.thinkbroadband.com/5MB.zip", "/sdcard/data/Cat03.jpg");;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

The code fails after printing JAVA_FILE_DOWNLOADER 1.5". and the stacktrace reads as the following:

 java.net.UnknownHostException: Unable to resolve host "ipv4.download.thinkbroadband.com": No address associated with hostname
     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
     at java.net.InetAddress.getAllByName(InetAddress.java:214)
     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
     at java.net.URL.openStream(URL.java:470)
     at worldline.atos.com.spicatemplate.Downloader.downloadFile(Downloader.java:27)
     at worldline.atos.com.spicatemplate.MyService$1.run(MyService.java:178)
     at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
     at libcore.io.Posix.getaddrinfo(Native Method)
     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    ... 15 more

Can some one please tell me what to do?

question from:https://stackoverflow.com/questions/65896764/android-openstream-fails-on-url-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...