2010-10-13 101 views
0

你好,伙计们, 即时在这里在奥地利你最近怎么样?安卓android文件下载

我有startet开发android和编写我自己的小应用程序,从服务器获取文件以在ListView中显示信息。我真的花了一天的时间来照顾从服务器获取文件的最好和最简单的方法。不过,我编写了程序并对其进行了测试 - 现在我在运行虚拟htc计算机时遇到了严重的错误消息。

它被称为:应用程序org.me.newspuler(进程org.me.newspuler)意外停止。请再试一次。

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

我真的不知道如何得到的提示问题,NewsPuler是我的应用程序, - 我想我需要一些更多的proffs帮助

我有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; 
    } 
} 

和这里(它的工作原理correclty如果你离开注释代码,但是当你取消注释 - 不

包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); 


    } 

} 

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

回答

0

在我看来就像你试图调用

al.add("Test"); 

当人是空的。这肯定会使应用程序崩溃。

改变人的声明

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; 
} 

}

,并确保你的清单包括Internet权限

<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> 

+0

我已经在一个类中测试了代码 - 似乎openStream有问题。当我仅测试URL被剪切时,我得到一个UnknownHostException。 – Martin 2010-10-14 09:34:22

+0

上面的编辑应该可以解决您的问题 – 2010-10-14 16:40:58

0

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

0

我已经在一个类中测试了代码 - 似乎openStream有问题。当我仅测试URL被剪切时,我得到一个UnknownHostException。