1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package com.paad.earthquake;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class EarthquakeListFragment extends ListFragment {
public AddNewQuake addNewQuake;
public interface AddNewQuake{
public void ajouterNewQuake(Quake _quake);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saveIinstanceState){
return inflater.inflate(R.layout.fragment_listview,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
refreshEarthquakes();
}
});
};
};
t.start();
}
private static final String TAG = "EARTHQUAKE";
private static final String TAG0 = "EARTHQUAKE0";
private static final String TAG1 = "EARTHQUAKE1";
private static final String TAG2 = "EARTHQUAKE2";
private static final String TAG3 = "EARTHQUAKE3";
private static final String TAG4 = "EARTHQUAKE4";
private void refreshEarthquakes() {
// Get the XML
URL url;
try {
String quakeFeed = getString(R.string.quake_feed);
url = new URL(quakeFeed);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the earthquake feed.
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
// Get a list of each earthquake entry.
NodeList nl = docEle.getElementsByTagName("entry");
if (nl != null && nl.getLength() > 0) {
for (int i = 1 ; i < nl.getLength(); i++) {
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String details = title.getFirstChild().getNodeValue();
Quake quake = new Quake(details);
// Process a newly found earthquake
addNewQuake.ajouterNewQuake(quake);
}
}
}
} catch (MalformedURLException e) {
Log.d(TAG0, "MalformedURLException", e);
} catch (IOException e) {
Log.d(TAG1, "IOException", e);
} catch (ParserConfigurationException e) {
Log.d(TAG2, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.d(TAG3, "SAX Exception", e);
}
catch (Exception e) {
Log.d(TAG4, "Exception simple", e);
}
finally {
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
addNewQuake = (AddNewQuake)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() +
" must implement AddNewQuake");
}
}
} |