使用 HttpClient 从 Web 读取文件

发布于 2024-12-13 04:22:39 字数 3250 浏览 2 评论 0原文

我正在尝试使用 httpclient 从网络读取文件,但收到此错误。我做错了什么?代码示例将不胜感激。

11-02 17:21:09.845: WARN/ActivityManager(67): Launch timeout has expired, giving up wake lock!
11-02 17:21:10.464: WARN/ActivityManager(67): Activity idle timeout for HistoryRecord{405591c0 com.jeffbreunig/.RetrievingAmazonXMLDataActivity}
11-02 17:21:15.534: DEBUG/dalvikvm(310): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 64ms
11-02 17:21:18.244: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
11-02 17:21:20.998: DEBUG/(1879): DID NOT WORK
11-02 17:21:21.344: INFO/ActivityManager(67): Displayed com.jeffbreunig/.RetrievingAmazonXMLDataActivity: +21s38ms

代码:

public class RetrievingAmazonXMLDataActivity extends Activity  {

    private static final String TAG = null;
    TextView txt;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        txt = (TextView) findViewById(R.id.txt);

        String urlToRead = "http://webdev4.matcmadison.edu/mbtest/mab/AmazonXML/FileList.txt";
        try {
        String fileList = getContent(urlToRead);

        txt.setText(fileList);

        } catch (Exception e) {
            Log.println(Log.DEBUG, TAG, "DUMP FILES DID NOT WORK");
        }



    }



    public String getContent(String url) {

        String responseBody;

        try {
            HttpClient client = new DefaultHttpClient();
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            HttpGet getMethod = new HttpGet(url);
            responseBody = client.execute(getMethod, responseHandler);
        }
        catch (Exception ex) {
        Log.println(Log.DEBUG, TAG,"DID NOT WORK");
            Toast.makeText(this, "Request failed: " + ex.getMessage(), 6000).show();
            return "Failure to read from server!";
        }

        return responseBody;

    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
    {
        String s = String.format("You chose [%s]", items[position]);
    }

    public void onNothingSelected(AdapterView<?>parent) {

    }


}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.jeffbreunig"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="9" />
        <uses-permission android:name="android.permission.INTERNET"/>

        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".RetrievingAmazonXMLDataActivity"
                      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>
    </manifest>

I am trying to read a file from the web with httpclient and am getting this error. What am I doing wrong? Code sample would be appreciated.

11-02 17:21:09.845: WARN/ActivityManager(67): Launch timeout has expired, giving up wake lock!
11-02 17:21:10.464: WARN/ActivityManager(67): Activity idle timeout for HistoryRecord{405591c0 com.jeffbreunig/.RetrievingAmazonXMLDataActivity}
11-02 17:21:15.534: DEBUG/dalvikvm(310): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 64ms
11-02 17:21:18.244: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
11-02 17:21:20.998: DEBUG/(1879): DID NOT WORK
11-02 17:21:21.344: INFO/ActivityManager(67): Displayed com.jeffbreunig/.RetrievingAmazonXMLDataActivity: +21s38ms

code:

public class RetrievingAmazonXMLDataActivity extends Activity  {

    private static final String TAG = null;
    TextView txt;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        txt = (TextView) findViewById(R.id.txt);

        String urlToRead = "http://webdev4.matcmadison.edu/mbtest/mab/AmazonXML/FileList.txt";
        try {
        String fileList = getContent(urlToRead);

        txt.setText(fileList);

        } catch (Exception e) {
            Log.println(Log.DEBUG, TAG, "DUMP FILES DID NOT WORK");
        }



    }



    public String getContent(String url) {

        String responseBody;

        try {
            HttpClient client = new DefaultHttpClient();
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            HttpGet getMethod = new HttpGet(url);
            responseBody = client.execute(getMethod, responseHandler);
        }
        catch (Exception ex) {
        Log.println(Log.DEBUG, TAG,"DID NOT WORK");
            Toast.makeText(this, "Request failed: " + ex.getMessage(), 6000).show();
            return "Failure to read from server!";
        }

        return responseBody;

    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
    {
        String s = String.format("You chose [%s]", items[position]);
    }

    public void onNothingSelected(AdapterView<?>parent) {

    }


}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.jeffbreunig"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="9" />
        <uses-permission android:name="android.permission.INTERNET"/>

        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".RetrievingAmazonXMLDataActivity"
                      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>
    </manifest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-12-20 04:22:39

您必须使用某种线程机制来通过 http 获取数据。我建议使用 AsyncTask

You have to use a some kind of threading mechanism for fetching the data over http. I suggest an AsyncTask.

画离情绘悲伤 2024-12-20 04:22:39

问题很可能是您在 Android 3.0 及更高版本上执行此操作,对吗?
在那里,您被迫使用 AsyncClass 来消费 Web 服务或从互联网下载某些内容,因为您的主 UI 线程不能无响应(长时间运行的代码“锁定”UI 线程的时间是有限制的);-)

Well propably the problem is you are doing this on Android 3.0 and above, right?
There, you are forced to use AsyncClass for consumig web services or downloading something from internet as your main UI thread can not be unresponsive (there is a limit for how long can the UI thread be "locked" by long running code ) ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文