꿈소년의 개발 이야기

Jsoup & encoding & charset & html 본문

Android Development

Jsoup & encoding & charset & html

꿈소년 2018. 1. 11. 17:47
반응형

how to encoding html using Jsoup in android.


1. request html


1
2
3
4
5
6
7
8
9
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(scrapUrl)
.header(HEADER_AGENT, USER_AGENT_CHROME)
.build();
 
Response response = client.newCall(request).execute();
htmlBody = response.body().byteStream();
 
cs


2. get Document & extract elements

1
2
Document doc = Jsoup.parse(htmlBody, null, url);
Elements elements = doc.select("meta[property^=og:]");
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (elements != null) {
    for (Element element : elements) {
        String property = element.attr("property");
        String content = element.attr("content");
 
        switch (property) {
            case "og:title":
                og.setTitle(content);
                break;
             .....
            default:
                break;
        }
    }
}
cs


end.