jericho 를 이용해서 해보는 건데요 .
예제를 그대로 하면 어떤건 jericho 라이브러리를 인식을 못해서 (빌드path 설정 다해줘도 안되더라구요 ㅜㅜ)
다른 예제를 해봤는데 이건 다른 오류인데
로그를 봐도 사실 먼지 잘 .. 모르겠어서 ㅜ 도움좀 얻고 싶습니다.
실행하면 Unfortunately, *** has stopped . 이 정겨운 메시지가 뜨더라구요 ㅜㅜ
한번만 봐주시면 엄청 도움이 될거같습니다 ㅜㅜ
package com.myandroid.please;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.List;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.FormControl;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class PleaseActivity extends Activity implements OnClickListener {
Button bt;
TextView resultView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.button1);
resultView = (TextView) findViewById(R.id.tv);
bt.setOnClickListener(this);
}
private void parse() {
try {
URL nURL = new URL("http://kr.searchcenter.yahoo.com/keyword");
InputStream html = nURL.openStream();
Source source = new Source(new InputStreamReader(html, "euc-kr"));
List<Element> atags = source.getAllElements(HTMLElementName.A);
for (int i = 0; i < atags.size(); i++) {
Element e = (Element)atags.get(i);
String href = e.getAttributeValue("href");
String txt = e.getContent().toString();
Log.e("2", "here:"+i+"ss"+txt);
Log.e("2", "href:"+href);
if(i==24){
resultView.setText(href);
}
}
List<FormControl> controls = source.getFormControls();
for (int i = 0; i < controls.size(); i++) {
FormControl c = controls.get(i);
String name = (String) c.getAttributesMap().get("name");
String type = (String) c.getAttributesMap().get("type");
String value = (String) c.getAttributesMap().get("value");
Log.e("1", "name:"+name+", type:"+type+", value:"+value);
}
Element table = (Element)source.getAllElements(HTMLElementName.TABLE).get(0);
Element tr = (Element) table.getAllElements(HTMLElementName.TR).get(0);
Element td = (Element) tr.getAllElements(HTMLElementName.TD).get(0);
Log.e("1", "table:0, tr:0, td:0 = "+ td.toString());
// id값으로 특정 요소를 가져옴
// Element e = (Element)source.getElementById("hogeId");
// Log.e("1", "chooce:"+e.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
parse();
}
}
--------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="눌러주세요" />
h
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
예제의 오류는 쓰레드 하나 띄워서 해보세요
아니면 빌드하는 에뮬레이터 기기의 OS 버젼을 2.3 버전 이하 버전으로 하시던지..