From f2792ced875402f7f7df84848130550fff27a638 Mon Sep 17 00:00:00 2001 From: Sadique Ali Date: Sat, 20 Jul 2013 17:00:14 -0700 Subject: [PATCH] Load just enough HTML to parse out the title. --- .../in/sdqali/sharewithtitle/PageDownloader.java | 5 +++-- .../in/sdqali/sharewithtitle/TitleParser.java | 16 ++++++++++++++++ .../in/sdqali/sharewithtitle/TitleRetriever.java | 9 ++------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleParser.java diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java index 09abaca..6d4b0e6 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java @@ -38,6 +38,9 @@ public class PageDownloader { String line = ""; while ((line = reader.readLine()) != null) { output += line; + if(TitleParser.retrieveTitle(output) != null) { + return output; + } } } catch (IOException e) { e.printStackTrace(); @@ -52,6 +55,4 @@ public class PageDownloader { } return output; } - - } \ No newline at end of file diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleParser.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleParser.java new file mode 100644 index 0000000..6399364 --- /dev/null +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleParser.java @@ -0,0 +1,16 @@ +package in.sdqali.sharewithtitle; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TitleParser { + public static String retrieveTitle(String html) { + Pattern p = Pattern.compile(".*?(.*?)", Pattern.DOTALL); + Matcher m = p.matcher(html); + String title = null; + while (m.find()) { + title = m.group(1); + } + return title; + } +} \ No newline at end of file diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java index 13ef954..348e697 100644 --- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java +++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java @@ -45,14 +45,9 @@ public class TitleRetriever { @Override protected void onPostExecute(String rawHtml) { - Log.d("Share With Title", "On post: " + rawHtml); - Pattern p = Pattern.compile(".*?(.*?).*?", Pattern.DOTALL); - Matcher m = p.matcher(rawHtml); - String title; + String title = TitleParser.retrieveTitle(rawHtml); String output = urlText; - while (m.find()) { - title = m.group(1); - Log.d("Share With Title", "Title: " + title); + if (title != null) { output = title + " " + urlText; } callback.onSuccess(output);