android中的数据连接:InputStream的“available”返回不同的值
下面的代码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您应该使用 Android 提供的 HttpClient,Java URL 类不够健壮。
喜欢:
Genereally you should use the HttpClient Android provides, the Java URL Class is not robust enough.
like: