android中的数据连接:InputStream的“available”返回不同的值

发布于 2024-12-04 15:47:18 字数 1183 浏览 0 评论 0原文

下面的代码在 Android 模拟器中有效,但在真实设备中无效。 inputstream.available() 在设备上返回 -1,而在模拟器上则返回一个 int 值(以千为单位)。有什么帮助吗?

代码:

try {
                URL url = new URL("http://www.google.com");
                Log.d(TAG,"Set URL");
                urlConnection = (HttpURLConnection) url.openConnection();
                //urlConnection.connect();
                Log.d(TAG,"Open connection");
                 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                Log.d(TAG,"get input");
                 str="Connect time out::"+urlConnection.getConnectTimeout()+
                     "\nContent length:"+urlConnection.getContentLength()+
                     "\nRead time out::"+ urlConnection.getReadTimeout()+
                     "\nResponse messgae::"+urlConnection.getResponseMessage()+
                     "\nAvailable bytes::::"+in.available();
                 Log.d(TAG,""+str);
                 read(in);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                urlConnection.disconnect();
            }

The below code works in Android emulator but not in real devices. The inputstream.available() returns -1 on device while on emulator, it returns an int value in thousands. Any help?

code:

try {
                URL url = new URL("http://www.google.com");
                Log.d(TAG,"Set URL");
                urlConnection = (HttpURLConnection) url.openConnection();
                //urlConnection.connect();
                Log.d(TAG,"Open connection");
                 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                Log.d(TAG,"get input");
                 str="Connect time out::"+urlConnection.getConnectTimeout()+
                     "\nContent length:"+urlConnection.getContentLength()+
                     "\nRead time out::"+ urlConnection.getReadTimeout()+
                     "\nResponse messgae::"+urlConnection.getResponseMessage()+
                     "\nAvailable bytes::::"+in.available();
                 Log.d(TAG,""+str);
                 read(in);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                urlConnection.disconnect();
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-12-11 15:47:18

一般来说,您应该使用 Android 提供的 HttpClient,Java URL 类不够健壮。

喜欢:

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI("http://w3mentor.com/"));
    HttpResponse response = client.execute(request);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

Genereally you should use the HttpClient Android provides, the Java URL Class is not robust enough.

like:

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI("http://w3mentor.com/"));
    HttpResponse response = client.execute(request);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文