본문 바로가기

카테고리 없음

HTML 파싱

package com.a1joproject.smart.html_reader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity {

private TextView tv;
EditText Parsing_Edit;
// 웹사이트 주소를 저장할 변수
String urlAddress = null;
Handler handler = new Handler(); // 화면에 그려주기 위한 객체

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 웹에서 html 읽어오기
// Handler 객체를 통해야만 화면을 그릴수 있다

Parsing_Edit = (EditText) findViewById(R.id.parsing_edit);
tv = (TextView)findViewById(R.id.textView1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadHtml(); // 웹에서 html 읽어오기
}
});
} // end of onCreate

void loadHtml() { // 웹에서 html 읽어오기
Thread t = new Thread(new Runnable() {
@Override
public void run() {
final StringBuffer sb = new StringBuffer();

try {
urlAddress = Parsing_Edit.getText().toString();
URL url = new URL(urlAddress);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();// 접속
if (conn != null) {
conn.setConnectTimeout(2000);
conn.setUseCaches(false);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
// 데이터 읽기
BufferedReader br = new BufferedReader(new InputStreamReader (conn.getInputStream(),"utf-8"));//"utf-8", "euc-kr"
while(true) {
String line = br.readLine();
if (line == null) break;
sb.append(line+"\n");
}
br.close(); // 스트림 해제
}
conn.disconnect(); // 연결 끊기
}
// 값을 출력하기
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(sb.toString());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start(); // 쓰레드 시작
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.a1joproject.smart.html_reader.MainActivity">
<EditText
android:id="@+id/parsing_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:background="#000"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp">

<TextView
android:id="@+id/LED_Power_Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Undetermined"/>
<Button
android:text="Test"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#ddd">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="loading..."/>
</ScrollView>
</LinearLayout>