Load just enough HTML to parse out the title.

This commit is contained in:
Sadique Ali 2013-07-20 17:00:14 -07:00
parent a780f08ca8
commit f2792ced87
3 changed files with 21 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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("<head>.*?<title>(.*?)</title>", Pattern.DOTALL);
Matcher m = p.matcher(html);
String title = null;
while (m.find()) {
title = m.group(1);
}
return title;
}
}

View File

@ -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("<head>.*?<title>(.*?)</title>.*?</head>", 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);