"流&QUOT意外结束; NetHtt prequest.execute过程中抛出(在上传文件时) [英] "unexpected end of stream" thrown during NetHttpRequest.execute() when uploading file

查看:267
本文介绍了"流&QUOT意外结束; NetHtt prequest.execute过程中抛出(在上传文件时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经被问了几个问题,但我还没有看到一个真正的答案。我有Android的code,它执行NetHtt prequest将图像发布到服务器,但它往往很快失败,像这样:

This has been asked in a couple questions but I've yet to see a real answer. I've got Android code that executes a NetHttpRequest to POST an image to a server, but it often fails quickly like so:

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)

我不明白这一点......为什么不能这样呢?鉴于这让我相信,它必须有一些做的HTTP库prematurely关闭连接回溯,但我不能确定原因。大多数相关的问题有完全一样的问题(例如,<一个href=\"http://stackoverflow.com/questions/12469588/uploading-jpeg-to-google-drive-from-android-app-unexpected-end-of-stream\">Uploading JPEG到谷歌驱动器从Android应用程序与QUOT;流&QUOT意外结束;但在任何情况下,)问题无人应答或提问者发现一些奇怪的解决方法,这似乎解决了他们(虽然他们可能刚刚幸运)。

I don't get it... why fail like this? Given the backtrace it makes me believe that it must have something to do with the HTTP library prematurely closing the connection, but I can't determine why. Most of the related questions have exactly the same problem (e.g. Uploading jpeg to Google Drive from Android app "unexpected end of stream", ) except in every case the question goes unanswered or the asker finds some strange workaround that seems to solve it for them (although they are likely just getting lucky).

我上传code不是什么特别的东西要么,只是一个AsyncTask的。它创建了Htt prequest并执行。它的工作原理完全正常的大部分时间,但有时莫名其妙地失败。是什么给了?

My upload code isn't anything special either, just an AsyncTask that creates an HttpRequest and executes it. It works perfectly fine most of the time, but other times it fails inexplicably. What gives?

推荐答案

好吧,我想通了这一点。

OK, I figured this out.

更多的调试后,我能够消除大量的变量和说服自己这个bug已经在核心HTTP库存在。一些谷歌上搜索发现,一些人在Android的实现HTTP引用的错误永葆(的 HTTPS://$c$c.google.com/p/googleappengine/issues/detail ID = 9291 ,的上的Galaxy Nexus 怪异EOFException类,等等)。

After more debugging I was able to eliminate a lot of variables and convinced myself this bug had to exist in the core HTTP library. Some googling found some people referencing bugs in Android's implementation of HTTP keep alive (https://code.google.com/p/googleappengine/issues/detail?id=9291, Weird EOFException on Galaxy Nexus, etc.).

所以我加入这个code到我的活动:

So I added this code to my activity:

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

这固定我的问题。恼人的解决办法,但它的工作原理。

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

这篇关于&QUOT;流&QUOT意外结束; NetHtt prequest.execute过程中抛出(在上传文件时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆