【问题标题】:android file download安卓文件下载
【发布时间】:2010-10-13 21:12:03
【问题描述】:

大家好, 我在奥地利,你好吗?

我已经开始开发 android 并编写自己的小应用程序,该应用程序从服务器获取文件以在 ListView 中显示信息。我真的花了一天的时间来研究从服务器获取文件的最佳和最简单的方法。但是我编写了程序并对其进行了测试——现在在运行虚拟 HTC 机器时出现了严重的错误消息。

调用:应用程序 org.me.newspuler(进程 org.me.newspuler)意外停止。请重试。

在此消息下方放置一个“强制关闭”按钮。

我真的不知道如何得到问题的提示,NewsPuler 是我的 APP,-我想我需要更多专家的帮助

我有 2 个类 - 一个用于下载文件(我认为这是错误的) - 一个用于主要活动 当编译器到达代码中创建“DownloadFile”实例的位置时...出现错误。

下载文件类:

package logik;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author raa
 */
public class DownloadFile {
    InputStream is = null;
    String link = null;
    BufferedReader d = null;
    URL url = null;
    String s;
    ArrayList<String> al = new ArrayList<String>();


    public  DownloadFile(String link) throws MalformedURLException{
        this.link = link;
        url = new URL(link);

        try {
            is = url.openStream();
        } catch (IOException ex) {
            Logger.getLogger(DownloadFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        d = new BufferedReader(new InputStreamReader(is));
        try {
            while ((s = d.readLine()) != null) {
                al.add(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(DownloadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList getArrayList(){
        return al;
    }
}

在这里(如果您留下注释代码,它可以正常工作,但是当您取消注释时 - 不是

包 org.me.newspuler;

import logik.DownloadFile;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author raa
 */
public class MainActivity extends Activity {
    ArrayList<String> al;
     DownloadFile df;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        ListView myListView = (ListView)findViewById(R.id.myListView);
//        try {
//            df = new DownloadFile("http://www.google.at/");
//
//        } catch (MalformedURLException ex) {
//            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
//        }

            //        al.add("Test");
            //         final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
            //                android.R.layout.simple_list_item_1, al);
            //
            //         myListView.setAdapter(aa);


    }

}

我真的很高兴能得到一些帮助! 来自奥地利的问候。

【问题讨论】:

    标签: android file download arraylist


    【解决方案1】:

    不要使用 java 日志记录类import java.util.logging.Logger;。使用 android logcat 类。

    【讨论】:

      【解决方案2】:

      我现在已经在一类中测试了代码 - 似乎 openStream 存在问题。当我只测试 URL 被截断时,我得到了一个 UnknownHostException。

      【讨论】:

        【解决方案3】:

        在我看来你想打电话

        al.add("Test");
        

        当 al 为空时。那肯定会使应用程序崩溃。

        将 al 的声明改为

        ArrayList<String> al = new ArrayList<String>();
        

        可能还有其他问题,但那是我真正关心的问题。

        我建议使用 android logcat 来获取有关错误的具体信息。

        我使用 eclipse 和插件进行开发。

        这里有教程。

        http://d.android.com/guide/developing/eclipse-adt.html

        进行适当的调试将帮助您分配。


        按照你说的试试这个。

        public class Tester extends Activity {
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ArrayList<String> list = null;
            try {
                list = DownloadFile("http://www.google.com");
            } catch (Exception e) {
                Log.i("File Download Test", e.getLocalizedMessage(), e);
            }
            if (list != null)
                for (String s : list)
                    Log.i("File Download Test", s);
        }
        
        public ArrayList<String> DownloadFile(String link) throws ClientProtocolException, IOException, URISyntaxException {
            ArrayList<String> al = new ArrayList<String>();
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(new URI(link));
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            InputStream is = null;
            try {
                is = entity.getContent();
            } catch (IOException ex) {
                Log.i("", ex.getMessage(), ex);
            }
            BufferedReader d = new BufferedReader(new InputStreamReader(is));
            String s = null;
            try {
                while ((s = d.readLine()) != null) {
                    al.add(s);
                }
            } catch (IOException ex) {
                Log.i("", ex.getMessage(), ex);
            }
            return al;
        }
        

        }

        并确保您的清单包含互联网权限

        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tester"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Tester"
                      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-permission
            android:name="android.permission.INTERNET"></uses-permission>
        

        【讨论】:

        • 我已经在一类中测试了代码 - 似乎 openStream 有问题。当我只测试 URL 被截断时,我得到了 UnknownHostException。
        猜你喜欢
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-19
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        相关资源
        最近更新 更多