- 전체
- android studio
- Android SDK
- Android NDK
- Android ADK
- Android Etc. Lib.
- Android dalvik VM
- Android 보안
- PhoneGap
- AOSP 개발
- MIT App Inventor 2
- kivy (python)
- ionic framework
- Kotlin (코틀린)
- React Native
Android SDK Android에서 jsoup를이용하여 HTML 파서(Parser)
2017.03.27 15:21
Android에서 jsoup를이용하여 HTML 파서(Parser)
새로운 안드로이드 프로젝트를 하나 생성하고…
http://jsoup.org/download 에서 jar 파일을 다운로드 받아 안드로드이 프로젝트 디렉토리의 libs 디렉토리에 복사해줍니다.


이제 안드로이드 프로젝트에서 jar 파일을 선택 후.. 마우스 오른쪽 버튼을 클릭하여 라이브러리로 추가해줍니다.

AndroidMenifest.xml 파일에 다음 한 줄을 추가해줍니다.
|
<uses-permission android:name="android.permission.INTERNET" /> |

activity_main.xml 파일에 텍스트 뷰 하나와 버튼 하나를 추가해주었습니다.
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
android:paddingBottom="@dimen/activity_vertical_margin"
-
tools:context=".MainActivity">
-
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:text=""
-
android:id="@+id/textView"
-
android:maxLines = "100"
-
android:scrollbars = "vertical"
-
android:layout_above="@+id/button" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="HTML 가져오기"
-
android:id="@+id/button"
-
android:layout_alignParentBottom="true"
-
android:layout_centerHorizontal="true" />
-
-
</RelativeLayout>
이제 MainActivity.java에 필요한 코드를 작성합니다.
-
package com.tistory.webnautes.jsouptest;
-
-
import android.os.AsyncTask;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.text.method.ScrollingMovementMethod;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
import org.jsoup.Jsoup;
-
import org.jsoup.nodes.Document;
-
import org.jsoup.nodes.Element;
-
import org.jsoup.select.Elements;
-
-
import java.io.IOException;
-
-
-
public class MainActivity extends AppCompatActivity {
-
-
private String htmlPageUrl = "http://www.daum.net/";
-
private TextView textviewHtmlDocument;
-
private String htmlContentInStringFormat;
-
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
-
textviewHtmlDocument = (TextView)findViewById(R.id.textView);
-
textviewHtmlDocument.setMovementMethod(new ScrollingMovementMethod());
-
-
Button htmlTitleButton = (Button)findViewById(R.id.button);
-
htmlTitleButton.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
-
jsoupAsyncTask.execute();
-
}
-
});
-
}
-
-
private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
-
-
@Override
-
protected void onPreExecute() {
-
super.onPreExecute();
-
}
-
-
@Override
-
protected Void doInBackground(Void... params) {
-
try {
-
Document doc = Jsoup.connect(htmlPageUrl).get();
-
Elements links = doc.select("a[href]");
-
-
for (Element link : links) {
-
htmlContentInStringFormat += (link.attr("abs:href")
-
+ "("+link.text().trim() + ")\n");
-
}
-
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
return null;
-
}
-
-
@Override
-
protected void onPostExecute(Void result) {
-
textviewHtmlDocument.setText(htmlContentInStringFormat);
-
}
-
}
-
-
-
}
실행시켜보면 www.daum.net에 있는 링크 주소와 링크 제목으로 이루어진 문자열들을 볼 수 있습니다.

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 12 |
App Inventor - MySQL interface 앱 인벤터 mysql 연결
| 졸리운_곰 | 2018.04.07 | 8002 |
| 11 |
Connect App Inventor to MySQL Database 앱 인벤터와 mysql 데이터베이스 연결
| 졸리운_곰 | 2018.04.07 | 7273 |
| 10 |
Create an API (PHP) 앱 인벤터와 php 통합
| 졸리운_곰 | 2018.04.07 | 630 |
| 9 |
Using the WebViewer Control in App Inventor 앱인터에서 웹뷰 컨트롤 사용
| 졸리운_곰 | 2018.03.24 | 512 |
| 8 |
WebView Javascript Processor for App Inventor 앱 인벤터 웹뷰 자바스크립트 인터페이스
| 졸리운_곰 | 2018.03.24 | 505 |
| 7 |
Creating a digital wallet application in App Inventor 2
| 졸리운_곰 | 2017.05.01 | 287 |
| 6 | Creating a Custom TinyWebDB Service | 졸리운_곰 | 2016.05.18 | 201 |
| 5 |
TinyDB is a lightweight document oriented database
| 졸리운_곰 | 2016.05.16 | 270 |
| 4 |
구글 앱엔진에 의한 소형웹데이터베이스의 접속방법 tinywebdb
| 졸리운_곰 | 2016.05.15 | 547 |
| 3 |
App inventor 멀티스크린, 스크린 이동, 다중화면,
| 졸리운_곰 | 2016.02.17 | 779 |
| 2 |
Communicating with Web APIs
| 졸리운_곰 | 2016.02.17 | 1080 |
| 1 |
JSON data on the Android mobile with App Inventor 2
| 졸리운_곰 | 2016.02.17 | 364 |

