我有Android的这段代码来获取InputStream
URL myURL = new URL("http://www.echo.msk.ru/interview/rss-audio.xml");
URLConnection ucon = myURL.openConnection();
InputStream is = ucon.getInputStream();
不知何故,对于那个网址http://www.echo.msk.ru/interview/rss-audio.xml我总是得到一个例外。但我可以在浏览器中打开该URL ...
java.net.UnknownHostException: Unable to resolve host "www.echo.msk.ru": No address associated with hostname
libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
Unable to resolve host "www.echo.msk.ru": No address associated with hostname
有没有办法在Java for Android中以其他方式获取该资源/ XML?
谢谢!
P.S。这是我的清单xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.developerworks.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MessageList"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="2" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest>
答案 0 :(得分:1)
使用AsyncTask创建HTTP get Request
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://www.echo.msk.ru/interview/rss-audio.xml");
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity);
Log.i(".......",_response);
日志的一部分
12-29 05:13:12.313: I/.......(3830): <?xml version='1.0' encoding='utf-8' ?>
12-29 05:13:12.313: I/.......(3830): <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
12-29 05:13:12.313: I/.......(3830): <channel>
12-29 05:13:12.313: I/.......(3830): <title>
12-29 05:13:12.313: I/.......(3830): Горячие интервью (звук) | Эхо Москвы
12-29 05:13:12.313: I/.......(3830): </title>
12-29 05:13:12.313: I/.......(3830): <link>http://echo.msk.ru/interview/</link>
12-29 05:13:12.313: I/.......(3830): <atom:link
12-29 05:13:12.313: I/.......(3830): href="http://echo.msk.ru/interview/rss-audio.xml %>"
12-29 05:13:12.313: I/.......(3830): rel="self"
12-29 05:13:12.313: I/.......(3830): type="application/rss+xml"
12-29 05:13:12.313: I/.......(3830): />
12-29 05:13:12.313: I/.......(3830): <description>
12-29 05:13:12.313: I/.......(3830): Эхо Москвы: Интервью
12-29 05:13:12.313: I/.......(3830): </description>
12-29 05:13:12.313: I/.......(3830): <image>
12-29 05:13:12.313: I/.......(3830): <title>Горячие интервью (звук) | Эхо Москвы</title>
12-29 05:13:12.313: I/.......(3830): <link>http://echo.msk.ru/interview/</link>
12-29 05:13:12.313: I/.......(3830): <url>http://echo.msk.ru/img/sys/logo_print.gif</url>
12-29 05:13:12.313: I/.......(3830): <width>121</width>
12-29 05:13:12.313: I/.......(3830): <height>80</height>
12-29 05:13:12.313: I/.......(3830): </image>