【问题标题】:Replace URLConnection with other code用其他代码替换 URLConnection
【发布时间】:2013-12-29 09:56:12
【问题描述】:

我有这段代码供 Android 获取 InputStream

URL myURL = new URL("http://www.echo.msk.ru/interview/rss-audio.xml");
URLConnection ucon = myURL.openConnection();
InputStream is = ucon.getInputStream();

不知何故,对于那个 URL 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?

谢谢!

附:这是我的清单 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>

【问题讨论】:

    标签: java android inputstream


    【解决方案1】:

    使用 AsyncTask 发出 HTTP 获取请求

    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>
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 2011-07-07
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多