Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HttpURLConnection::getInputStream()で取得したInputStreamが空の場合について

Last updated at Posted at 2014-11-12

Android 4.4.4 (Nexus5) で確認した現象で、
HttpURLConnection::getInputStream()で取得したInputStreamから
データを読み込もうとしたが、何も取得できなかった。
そのInputStreamのavailable()は0となっていた。
また、HttpURLConnectionのgetContentLength()は-1となっていた。

通信を行っているサーバーのほうではちゃんとデータを返しており、
返却するデータサイズを小さくしてみると、それは受信できた。
また、2.3端末で同じアプリを実行し、同じサーバーに接続すると問題なくデータを受信した。

原因はアプリや端末側ではなくサーバー側にあり、
サーバーでContent-Lengthヘッダを明示的に出力するようにしたところ、
データを取得することができるようになった。

要因も対応も複数あるかもしれない現象だが、一例として自分が踏んだものを残しておく。

※2014/11/25 追記

Android 5.0では、getContentLength()が正しく取得できてもavailable()は0になるよう。
なので、InputStreamからデータを取得する際には、available()は期待しないようにする。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?