我正在尝试在 android 的异步任务中进行下载。不幸的是,我收到“unknownHost”错误。对我来说,我似乎拥有适当的权限,但我仍然收到此错误。下面是我的权限、我的错误和功能。对我做错了什么有任何想法吗?
非常感谢你。
克雷格
11-27 21:51:34.545: D/ImageManager(9381): Error: java.net.UnknownHostException:
public void downloadFromUrl(String strUrl, String fileName) { // this is the downloader
// method
try {
File parentDirectory= new File(PATH);
if (!parentDirectory.exists()) {
System.err
.println("It seems like parent directory does not exist...");
if (!parentDirectory.mkdirs()) {
System.err.println("And we cannot create it...");
// we have to return, throw or something else
}
}
URL url = new URL(strUrl);
File file = new File(PATH + fileName);
if (!file.exists()) {
file.createNewFile();
}
long startTime = System.currentTimeMillis();
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager",
"download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.craig.musicapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
. <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<application
android:icon="@drawable/jerrygarciahand"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SearchMusic"
android:label="@string/search_music"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ConcertDetailActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ItemListActivity" />
</activity>
<activity
android:name=".AppInfo"
android:label="@string/title_item_list" >
</activity>
</application>
</manifest>