当尝试在android 2.1 平台上打开连接时,HttpUrlConnection.getInputStream 方法总是抛出IOException、FileNotFound 异常。该代码在 2.2 和 2.3 版本的 Android 中按预期工作。我有一个创建新连接的标准方法。调用获取输入流后立即抛出错误。请注意,所有试图建立的连接都是“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;
}
最佳答案
如果它在 2.1 上打开连接 - 我看不出它不能在 > 2.1 上工作的原因。
可能是您的 Android.manifest
中缺少权限。如果是,你试过了吗this ?
关于android - HttpURLConnection.getInputStream 总是在 android 2.1 中抛出 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716979/