IOException - 读取html文件时意外的流结束

时间:2013-08-18 09:37:36

标签: java android download ioexception

我正在尝试下载php页面的内容(这只是3个单词),但我得到了java.io.IOException: unexpected end of stream。这是下载代码:

// Download file list
        String zipListUrl = baseUrl + ziplist;

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(zipListUrl);
        try {
            HttpResponse response = client.execute(request);
            // txtResult.setText(HttpHelper.request(response));
            InputStream in = response.getEntity().getContent();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));


            String line = null;
            String result = null;
            while ((line = reader.readLine()) != null) {
                // fileNames.add(line);
                result += line;
            }
            in.close();
            // Turn arraylist into simple array
            // finalFileNames = (String[]) fileNames.toArray();

            Log.i(LOG_TAG, /* Arrays.toString(finalFileNames) */result);

        } catch (Exception ex) {
            ex.printStackTrace();
            Log.d(LOG_TAG, "Exception ex1  = " + ex.toString());
        }

以下是例外:

08-18 11:11:04.312: W/HttpTransport(3101): unexpected end of stream
08-18 11:11:04.312: W/HttpTransport(3101): java.io.IOException: unexpected end of stream
08-18 11:11:04.312: W/HttpTransport(3101):  at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
08-18 11:11:04.312: W/HttpTransport(3101):  at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:83)
08-18 11:11:04.312: W/HttpTransport(3101):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895)
08-18 11:11:04.312: W/HttpTransport(3101):  at net.zedge.android.api.request.BaseApiRequest.run(BaseApiRequest.java:50)
08-18 11:11:04.312: W/HttpTransport(3101):  at net.zedge.android.api.request.BaseApiRequest$1.run(BaseApiRequest.java:84)
08-18 11:11:04.312: W/HttpTransport(3101):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
08-18 11:11:04.312: W/HttpTransport(3101):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-18 11:11:04.312: W/HttpTransport(3101):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-18 11:11:04.312: W/HttpTransport(3101):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-18 11:11:04.312: W/HttpTransport(3101):  at java.lang.Thread.run(Thread.java:841)
08-18 11:11:04.342: W/HttpTransport(3101): unexpected end of stream
08-18 11:11:04.342: W/HttpTransport(3101): java.io.IOException: unexpected end of stream
08-18 11:11:04.342: W/HttpTransport(3101):  at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
08-18 11:11:04.342: W/HttpTransport(3101):  at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:83)
08-18 11:11:04.342: W/HttpTransport(3101):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895)
08-18 11:11:04.342: W/HttpTransport(3101):  at net.zedge.android.api.request.BaseApiRequest.run(BaseApiRequest.java:50)
08-18 11:11:04.342: W/HttpTransport(3101):  at net.zedge.android.api.request.BaseApiRequest$1.run(BaseApiRequest.java:84)
08-18 11:11:04.342: W/HttpTransport(3101):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
08-18 11:11:04.342: W/HttpTransport(3101):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-18 11:11:04.342: W/HttpTransport(3101):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-18 11:11:04.342: W/HttpTransport(3101):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-18 11:11:04.342: W/HttpTransport(3101):  at java.lang.Thread.run(Thread.java:841)

我尝试过调试,result的最后一个值是这样的: null<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL /ziplist.php was not found on this server.</p><p>Additionally, a 404 Not Founderror was encountered while trying to use an ErrorDocument to handle the request.</p></body></html>

欢迎任何想法!

1 个答案:

答案 0 :(得分:1)

似乎Content-Length设置不正确(the source显示FixedLengthOutputStream.close在大小不匹配时抛出IOException)

此外,在您的修改中,您显示您的网页返回的是“404”,表示您没有使用ziplist.php的正确网址。仔细检查您正在使用的网址:)