/** Helper Methods: gets the source of an html file */ public List<String> executeHttpGet(String urlString) { List<String> lines = new LinkedList<String>(); try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) lines.add(inputLine); in.close(); } catch (Exception e) { Log.e("httpGet", "Could not load Html."); e.printStackTrace(); } return lines; }
private void updateImageForArticle(Article article) throws SQLException { try { List<String> html = executeHttpGet(Constants.pathToArticleDescription + article.getId()); LinkedList<String> possibleLinks = new LinkedList<String>(); Pattern pattern = Pattern.compile("\"[^\"]*.jpg"); for (String line : html) { Matcher m = pattern.matcher(line); while (m.find()) { String possibleLink = m.group().substring(1, m.group().length()); possibleLinks.add(possibleLink); } } if (possibleLinks.size() > 0) article.setImageUrl(possibleLinks.get(0)); else article.setImageUrl(""); } catch (Exception e) { Log.i("UpdataDatabase", "Couldn't get Sites html"); e.printStackTrace(); article.setImageUrl(""); } article.createOrUpdate(); }