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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package com.example.chap_7_earthquake_part_2_c;
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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
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.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class EarthquakeListFragment extends ListFragment {
ArrayAdapter<Quake> aa;
ArrayList<Quake> earthquakes = new ArrayList<Quake>();
public String location = "";
public String depth;
public static String TAG = "EARTHQUAKE_ListFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.liste_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<Quake>(getActivity(), layoutID , earthquakes);
setListAdapter(aa);
refreshEarthquakes();
}
//--------------------------------------------------------
public 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();
// Clear the old earthquakes
earthquakes.clear();
// Get a list of each earthquake entry.
NodeList nl = docEle.getElementsByTagName("entry");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
Element when = (Element)entry.getElementsByTagName("updated").item(0);
Element link = (Element)entry.getElementsByTagName("link").item(0);
Element summary = (Element)entry.getElementsByTagName("summary").item(0);
Element g = (Element)entry.getElementsByTagName("georss:point").item(0);
Element d = (Element)entry.getElementsByTagName("georss:elev").item(0);
if (title!= null) Log.d("pas null ","pas null title");
Log.d("vanleur de Title ",title.getFirstChild().getNodeValue());
if (when!= null) Log.d("pas null ","pas null when");
if (link!= null) {
Log.d("pas null ","pas null link");
}
if (summary!= null) Log.d("pas null ","pas null summary");
if (g!= null) Log.d("pas null ","pas null");
// Le titre
//----------
String details = title.getFirstChild().getNodeValue();
if (details.equals("Data Feed Deprecated")) details = "USGS M 2.5+ Earthquakes";
// La date
// -------
String dt = when.getFirstChild().getNodeValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
Date qdate = new GregorianCalendar(0,0,0).getTime();
try {
qdate = sdf.parse(dt);
} catch (ParseException e) {
}
// Localisation : latitude Longitude
// -----------------------------------
if (g!= null){
location = g.getFirstChild().getNodeValue();
Log.d("CI ","ICI");
}
// Profondeur
// -----------
if (d!= null){
depth = d.getFirstChild().getNodeValue();
Log.d("Depth ",depth);
}
// Magnitude
String magnitudeString = details.split(" ")[1];
int end = magnitudeString.length()-1;
String magnitude_S = ((magnitudeString.substring(0, end))+ "0").trim();
double magnitude = Double.parseDouble(magnitude_S);
Quake quake = new Quake(qdate,details,location,magnitude,depth);
// Process a newly found earthquake
addNewQuake(quake);
}
}
}
} catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.d(TAG, "IOException", e);
} catch (ParserConfigurationException e) {
Log.d(TAG, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.d(TAG, "SAX Exception", e);
}
finally {
}
}
private void addNewQuake(Quake _quake) {
MainActivity earthquakeActivity = (MainActivity)getActivity();
if (_quake.getMagnitude() > earthquakeActivity.minimumMagnitude) {
// Add the new quake to our list of earthquakes.
earthquakes.add(_quake);
}
// Notify the array adapter of a change.
aa.notifyDataSetChanged();
}
} |
Partager