android file download
Hallo guys, im here in austria how are you doing?
i have startet developing android and writing my own small app that gets files from a server to display the information in a ListView. I really spent a day to look after the best and easiest way to get files from a server. However i wrote t开发者_运维问答he program and tested it - and now i have a serious error message when running the virtual htc machine.
Its called: The application org.me.newspuler (process org.me.newspuler) has stopped unexpectedly. Please try again.
Under this message is put a "Force Close" Button.
I really dont know how to get a hint to the problem, NewsPuler is my APP, - i think I need help from some more proffs
I have 2 classes - one for the download file(i think here is the mistake) - one for the Main Activity When the compiler comes to the point in the code where the instance from "DownloadFile" is created... the error appears.
the download file class:
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;
}
}
And here (it works correclty if you leave the commented code, but when you uncomment it - not)
package 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);
}
}
I would be really glad to get some help! Kindly Regards from Austria.
Do NOT use the java logging classes import java.util.logging.Logger;
. Use the android logcat class.
i have tested the code now in one class - seems that there is a problem with the openStream. i got a UnknownHostException when i testet only the URL snipped.
looks to me like your trying to call
al.add("Test");
when al is null. That would definitely crash the app.
change the declaration of al to
ArrayList<String> al = new ArrayList<String>();
there could be other issues but thats the one which really sticks out to me.
i recommend using android logcat to get specific information about the error.
I use eclipse and plugins for development.
a tutorial is here.
http://d.android.com/guide/developing/eclipse-adt.html
having proper debugging will help you allot.
in response to what you said try this.
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;
}
}
and make sure your manifest includes the internet permission
<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>
精彩评论