"unexpected end of stream" that is called during NetHttpRequest.execute () when the file is loaded

They asked a couple of questions, but I have not yet found a real answer. I have an Android code that runs NetHttpRequest to send an image to a server, but it often doesn’t work quickly like this:

unexpected end of stream java.io.IOException: unexpected end of stream at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58) at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1009) at com.test.MyActivity$ImageUploadTask.doInBackground(MyActivity.java:870) at com.test.MyActivity$ImageUploadTask.doInBackground(MyActivity.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) 

I don’t understand ... why so bad? Given the feedback, it seems to me that it should have something to do with the HTTP library, closing the connection prematurely, but I cannot determine why. Most related questions have exactly the same problem (for example, downloading jpeg to Google Drive from an Android application and unexpected end of stream ), unless the question goes unanswered or the crawler finds some strange workaround that seems to be solves it for them (although probably they are just lucky).

My download code is not special, just AsyncTask that creates the HttpRequest and executes it. In most cases, it works great, but in other cases, it is inexplicable. What gives?

0
source share
1 answer

OK, I get it.

After further debugging, I was able to eliminate many variables and convinced myself that this error must exist in the underlying HTTP library. Some Google search engines have found that some people cite errors in the implementation of the Android HTTP protocol keep alive ( https://code.google.com/p/googleappengine/issues/detail?id=9291 , Strange EOFException on Galaxy Nexus , etc. .d.).

Therefore, I added this code to my activity:

 System.setProperty("http.keepAlive", "false"); 

And that fixed my problem. Annoying workaround, but it works.

+1
source

Source: https://habr.com/ru/post/944406/


All Articles