HttpURLConnection.getInputStream 在 android 2.1 中总是抛出 IOException
当尝试在android 2.1平台上打开连接时,HttpUrlConnection.getInputStream方法总是抛出IOException、FileNotFound异常。该代码在 Android 2.2 和 2.3 版本中按预期工作。我有一个创建新连接的标准方法。当调用获取输入流时,错误会立即抛出。请注意,尝试建立的所有连接都是“http://something/something”。
public static URLConnection createConnection(String urlStr, Boolean useAuthentication, Boolean setOutput){
Log.i(GpodRoid.LOGTAG, "Creating Connection to " + urlStr);
URLConnection conn = null;
try {
conn = new URL(urlStr).openConnection();
conn.setDoOutput(setOutput);
conn.setDoInput(true);
if(useAuthentication){
String auth = GpodRoid.prefs.getUsername() + ":" + GpodRoid.prefs.getPassword();
String encoded = Base64.encodeBytes(auth.getBytes());
conn.setRequestProperty("Authorization","basic " + encoded);
}
}
catch (Exception e) {
Log.e(GpodRoid.LOGTAG, "Error creating Connection: " + stackTrace(e));
}
return conn;
}
When attempting to open a connection on the android 2.1 platform, the HttpUrlConnection.getInputStream method always throws a IOException, FileNotFound exception. The code works as expected in 2.2 and 2.3 version of Android. I have a standard method for creating a new connection. The error is thrown immediately after this when get input stream is called. As a note, all of the connections attempting to be made are "http://something/something".
public static URLConnection createConnection(String urlStr, Boolean useAuthentication, Boolean setOutput){
Log.i(GpodRoid.LOGTAG, "Creating Connection to " + urlStr);
URLConnection conn = null;
try {
conn = new URL(urlStr).openConnection();
conn.setDoOutput(setOutput);
conn.setDoInput(true);
if(useAuthentication){
String auth = GpodRoid.prefs.getUsername() + ":" + GpodRoid.prefs.getPassword();
String encoded = Base64.encodeBytes(auth.getBytes());
conn.setRequestProperty("Authorization","basic " + encoded);
}
}
catch (Exception e) {
Log.e(GpodRoid.LOGTAG, "Error creating Connection: " + stackTrace(e));
}
return conn;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它在 2.1 上打开连接 - 我看不出它不应该在 > 上工作的原因2.1.
可能是您的 Android.manifest 中缺少权限。如果是,您是否尝试过这个?
If it opens the connection on 2.1 - I don't see a reason why it shouldn't work on > 2.1.
Could it be a missing permission in your
Android.manifest
. If yes, have you tried this ?