Android 通過網址url獲取網址標題
阿新 • • 發佈:2018-12-08
大多數場景下,我們需要展示一個網頁並獲取標題時,我們會用webview去load這個url,在onReceiveTitle裡獲取它的標題。但在某些場景下,我們想直接拿到標題但並不想展示這個網頁,畢竟webView也是很吃效能的。 首先我們得獲取這個url的html文字,然後從html文字中截取出title,這裡我們用兩種方式來獲取
第一種方式
獲取html文字程式碼
public static String getHtmlContent(String urlpath) throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn=null ;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6 * 1000);
conn.setRequestMethod("GET");
conn.setInstanceFollowRedirects(true);
if (conn.getResponseCode() == 200) {
LogUtil.logE("success===200" );
InputStream inputStream = conn.getInputStream();
byte[] data = readStream(inputStream);
String html = new String(data);
return html;
}else if(conn.getResponseCode()==301||conn.getResponseCode()==302){//重定向
LogUtil.logE("重定向========" +conn.getResponseCode());
String nestUrl=conn.getHeaderField("Location");
LogUtil.logE("重定向==="+nestUrl);
return getHtmlContent(nestUrl);
}
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//最後將conn斷開連線
if (conn != null) {
conn.disconnect();
}
}
return null;
}
public static byte[] readStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
inputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
有些url的getResponseCode會返回301,302重定向,需要拿到重定向後的url繼續獲取html文字。拿到html文字後,擷取title標籤
//擷取title標籤獲取標題
public static String getTitle(String html){
LogUtil.logE("html==========="+html);
if(!TextUtils.isEmpty(html)){
int rightPoint=html.indexOf("</title>");
if(rightPoint<0)return"";
String sub =html.substring(0,rightPoint);
int leftPoint=sub.lastIndexOf(">");
if(leftPoint<0)return"";
String title=sub.substring(leftPoint+1,sub.length());
return title;
}
return "";
}
第二種方式
第一種方式獲取方式存在缺陷性,比如不同網站title的編碼格式不一,有些網址截取出來的title是亂碼。也有反饋說,有些網址conn.getResponseCode() == 200時,獲取到的html文字是空。(這個我自己倒是沒遇到過。。。)
這第二種方式是使用jsoup來獲取。
新增依賴 compile 'org.jsoup:jsoup:1.9.2'
先得到head標籤再拿到title標籤
public static String getWebTitle(String url){
try {
//還是一樣先從一個URL載入一個Document物件。
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("head");
Elements titlelinks=links.get(0).select("title");
return titlelinks.get(0).text();
}catch(Exception e) {
return "";
}
}
果然還是第三方叼,經實驗, Document doc = Jsoup.connect(url).get()已經處理過重定向了。 個人推薦使用第二種方法。 PS:網路操作請在子執行緒處理 O_O !