为什么这个错误liborce.net.http.httpurlconnectionimpl.getinputstream发生在android?

时间:2015-07-19 03:35:28

标签: java android urlconnection

当我在模拟器(Android 5.0)中测试我的应用程序时,它成功运行并获取我想要的图片并且getRequestCode为200.但是当我在我的galaxy s3和moto xt889中运行它时,此错误发生了。并且getRequestCode是404。

public class DownloadService {
private static String TAG = "DownloadService";
public static final int IO_BUFFER_SIZE = 8 * 1024;
private static final String CACHE_FILENAME_PREFIX = "cache_";
private static ExecutorService SINGLE_TASK_EXECUTOR = null;
private static ExecutorService LIMITED_TASK_EXECUTOR = null;
private static final ExecutorService FULL_TASK_EXECUTOR = null;
private static final ExecutorService DEFAULT_TASK_EXECUTOR;
private Pattern p;
private Matcher m;
private static Object lock = new Object();
static {
    // SINGLE_TASK_EXECUTOR = (ExecutorService)
    // Executors.newSingleThreadExecutor();
    LIMITED_TASK_EXECUTOR = (ExecutorService) Executors
            .newFixedThreadPool(1);
    // FULL_TASK_EXECUTOR = (ExecutorService)
    // Executors.newCachedThreadPool();
    DEFAULT_TASK_EXECUTOR = LIMITED_TASK_EXECUTOR;
};

DownloadStateListener listener;

private String downloadPath;


private List<String> listURL;
// 下载个数
private int size = 0;

private Context context;


public interface DownloadStateListener {
    public void onFinish();

    public void onFailed();
}

public DownloadService(String downloadPath, List<String> listURL,
        DownloadStateListener listener, Context context) {
    this.context = context;
    this.downloadPath = downloadPath;
    this.listURL = listURL;
    this.listener = listener;
    p = Pattern.compile(".*com/(.*)");
}


public void setDefaultExecutor() {

}


public void startDownload() {

    File downloadDirectory = new File(downloadPath);
    if (!downloadDirectory.exists()) {
        downloadDirectory.mkdirs();
    }

    for (final String url : listURL) {

        try {

            DEFAULT_TASK_EXECUTOR.execute(new Runnable() {

                @Override
                public void run() {
                    downloadBitmap(url);
                }
            });
        } catch (RejectedExecutionException e) {
            e.printStackTrace();
            Log.e(TAG, "thread pool rejected error");
            listener.onFailed();
        } catch (Exception e) {
            e.printStackTrace();
            listener.onFailed();
        }

    }

}


private void downloadBitmap(String urlString) {
    Matcher m = p.matcher(urlString);
    OutputStream fos=null;
    m.find();

    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;

    try {
        fos = context.openFileOutput(m.group(1), Context.MODE_PRIVATE);

        final URL url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.getResponseCode();
        Log.d("dfdf", urlConnection.getResponseCode()+"");


        final InputStream in = new BufferedInputStream(
                urlConnection.getInputStream(), IO_BUFFER_SIZE);
        out = new BufferedOutputStream(fos, IO_BUFFER_SIZE);

        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }

        statDownloadNum();

    } catch (final IOException e) {

        Log.e(TAG, "download " + urlString + " error");
        e.printStackTrace();
        listener.onFailed();

    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        if (out != null) {
            try {
                out.close();
                fos.close();
            } catch (final IOException e) {
                Log.e(TAG, "Error in downloadBitmap - " + e);
            }
        }
    }

}




private void statDownloadNum() {
    synchronized (lock) {
        size++;
        if (size == listURL.size()) {
            Log.d(TAG, "download finished total " + size);

            DEFAULT_TASK_EXECUTOR.shutdownNow();

            listener.onFinish(); 
        }
    }
}

}

错误是

java.io.FileNotFoundException: http://dnight-math.stor.sinaapp.com/理综1_img030.jpg
 at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
 at util.DownloadService.downloadBitmap(DownloadService.java:159)
 at util.DownloadService.access$0(DownloadService.java:136)
 at util.DownloadService$1.run(DownloadService.java:114)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 at java.lang.Thread.run(Thread.java:841)

为什么当我使用真正的手机时会发生这种错误?

1 个答案:

答案 0 :(得分:0)

这是因为服务器返回HTTP.4xx或HTTP.5xx错误。如果要获取错误页面,则应使用HttpURLConnection.getErrorStream()方法。 像这样:

InputStream inputStream = null;
try {
   inputStream = urlConnection.getInputStream();
} catch(FileNotFoundException e) {
   inputStream = urlConnection.getErrorStream();
}