I am trying to download the content of a php page (it is just 3 words), but I am getting java.io.IOException: unexpected end of stream
. This is the code for downloading:
// 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());
}
And here is the exception:
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)
I tried debugging, and the last value of result
is this:
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>
Any ideas are welcomed!
It would seem that Content-Length is set incorrectly (the source shows that FixedLengthOutputStream.close throws IOException on a size mismatch)
Also, in your edit you're showing that your page is returning a '404, which indicates that you're not using the correct URL for
ziplist.php
. Double check the URL you're using :)