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
|
class htmlParser extends AsyncTask<Void, Void, String [][]> {
private String error=null;
@Override
protected String [][] doInBackground(Void... params){
try {
Document myDoc = Jsoup.connect("http://www.mon_site.fr").get();
Elements myArticles = myDoc.select("div.list");
for (Element myArticle : myArticles) {
Elements balisesA = myArticle.select("a");
articles = new String[balisesA.size()][5];
for (int i = 0; i < balisesA.size(); i++) {
articles[i][0] = balisesA.get(i).getElementsByClass("title").text();
articles[i][1] = balisesA.get(i).getElementsByClass("price").text();
articles[i][2] = balisesA.get(i).attr("href");
articles[i][3] = balisesA.get(i).getElementsByClass("placement").text();
Elements img = balisesA.get(i).select("img");
for (Element el : img) {
String src = el.absUrl("src");
articles[i][4] = src;
}
}
}
}
catch (Exception e){
error=e.getMessage();
e.printStackTrace();
}
return articles;
}
protected void onPostExecute(String[][] result){
super.onPostExecute(result);
ListView maAdsListView = (ListView) findViewById(R.id.listView);
ArrayList<HashMap<String, Object>> myAdsList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for (int i = 0; i < articles.length; i++){
map = new HashMap<String, Object>();
map.put("titre", articles[i][0]);
map.put("description", articles[i][1]);
if (articles[i][4] == null){
map.put("image", String.valueOf(R.drawable.image_indispo));
} else {
Utiliser la librairie urlimageviewhelper ICI pour afficher les images distantes
}
myAdsList.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(getBaseContext(), myAdsList, R.layout.affichage_ads,
new String[] {"image", "titre", "description"}, new int[] {R.id.ad_img, R.id.ad_titre, R.id.ad_description});
maAdsListView.setAdapter(mSchedule);
}
} |
Partager