我有以下情况:我需要从Internet获取xml文件,然后从该文件获取歌词。 Here's the url of this xml。这是我编写并应该执行的代码。
private static class NetworkLyricsDownload extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=Eminem&song=Lose%20Yourself");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(getInputStream(url), "UTF-8");
boolean insideItem = false;
int eventType = xmlPullParser.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xmlPullParser.getName().equalsIgnoreCase("lyric")){
insideItem = true;
Log.i(getClass().getName(), xmlPullParser.nextText());
}
}
eventType = xmlPullParser.next();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private InputStream getInputStream(URL url){
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
但是当我运行该应用程序时,出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: asus.example.com.player, PID: 14458
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalArgumentException: is == null
at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1636)
at asus.example.com.player.UploadLyricsActivity$NetworkLyricsDownload.doInBackground(UploadLyricsActivity.java:114)
at asus.example.com.player.UploadLyricsActivity$NetworkLyricsDownload.doInBackground(UploadLyricsActivity.java:86)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
我在清单文件(<uses-permission android:name="android.permission.INTERNET"/>
中具有权限
),但仍然无济于事。那么,怎么了,我该如何解决?
UPD
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="asus.example.com.player"
android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">
<activity android:name=".SongLyricsActivity"/>
<activity android:name=".UploadLyricsActivity" />
<activity
android:name=".ListOfSongsActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService" />
</application>
</manifest>