当尝试在android 2.1平台上打开连接时,HttpUrlConnection.getInputStream方法总是抛出IOException,FileNotFound异常。该代码在2.2和2.3版本的Android中按预期工作。我有一个创建新连接的标准方法。调用get输入流后,会立即抛出错误。请注意,尝试制作的所有连接都是“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;
}