안녕하세요 오늘은 HTML Dom Parse를 통하여 앱을 만들어 보았습니다
Android Native App 을 통하여 네이버와 다음 메인화면의 실시간 검색어를 parse를 해오는 기능을 보도록 하겠습니다.
1. 안드로이드 설치
2. Jsoup 다운로드 [바로가기]
3. 다운받은 라이브버리 추가하기
- app > lib 드래그 앤드 드랍으로 추가
4. 소스코드
MainActivity.java
package com.example.minju.myapplication;
import android.annotation.SuppressLint; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; 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 { TextView textView; //결과를 띄어줄 TextView TextView reload; //reload버튼
TextView textView2; //결과를 띄어줄 TextView TextView reload2; //reload버튼
Elements contents; Document doc = null; String Top10;//결과를 저장할 문자열변수
Elements contents2; Document doc2 = null; String Top102;//결과를 저장할 문자열변수
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textBox); reload = (TextView) findViewById(R.id.reload);
textView2 = (TextView) findViewById(R.id.textBox2); reload2 = (TextView) findViewById(R.id.reload2);
new AsyncTask() { @Override protected Object doInBackground(Object[] params) { try { doc = Jsoup.connect("https://www.naver.com/").get(); contents = doc.select("span.ah_k"); } catch (IOException e) { e.printStackTrace(); }
Top10 = ""; int cnt = 0; for(Element element: contents) { cnt++; Top10 += cnt+". "+element.text() + "\n"; if(cnt == 10) break; }
///////////////////////////////// try { doc2 = Jsoup.connect("https://www.daum.net/").get(); contents2 = doc2.select("div span.txt_issue a.link_issue"); } catch (IOException e) { e.printStackTrace(); }
Top102 = ""; int cnt2 = 0; for(Element element2: contents2) { cnt2++; if(cnt2%2 == 0) { Top102 += cnt2/2 + ". " + element2.text() + "\n"; } if(cnt2 == 20) break; } return null;
}
@Override protected void onPostExecute(Object o) { super.onPostExecute(o); Log.i("TAG",""+Top10); Log.i("TAG",""+Top102); textView.setText(Top10); textView2.setText(Top102); } }.execute();
reload.setOnClickListener(new View.OnClickListener() {//onclicklistener를 연결하여 터치시 실행됨 @SuppressLint("StaticFieldLeak") @Override public void onClick(View v) { new AsyncTask() { @Override protected Object doInBackground(Object[] params) { try { doc = Jsoup.connect("https://www.naver.com/").get(); contents = doc.select("span.ah_k"); } catch (IOException e) { e.printStackTrace(); }
Top10 = ""; int cnt = 0; for(Element element: contents) { cnt++; Top10 += cnt+". "+element.text() + "\n"; if(cnt == 10) break; } return null; }
@Override protected void onPostExecute(Object o) { super.onPostExecute(o); Log.i("TAG",""+Top10); textView.setText(Top10); } }.execute(); } }); }} |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:id="@+id/reload" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#bae53c" android:gravity="center" android:text="네이버 실시간 키워드" android:textSize="40sp" android:textStyle="bold" />
<ScrollView android:layout_width="match_parent" android:layout_height="200dp">
<TextView android:id="@+id/textBox" android:layout_width="match_parent" android:layout_height="match_parent" android:text="text" android:textSize="18sp" /> </ScrollView>
<TextView android:id="@+id/reload2" android:layout_width="match_parent" android:layout_height="65dp" android:background="@android:color/holo_orange_dark" android:gravity="center" android:text="다음 실시간 키워드" android:textSize="40sp" android:textStyle="bold" />
<ScrollView android:layout_width="match_parent" android:layout_height="200dp">
<TextView android:id="@+id/textBox2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="text" android:textSize="18sp" /> </ScrollView> </LinearLayout>
|
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.minju.myapplication"> <uses-permission android:name="android.permission.INTERNET" /> |
5. apk만들기
-Build > Build Apk 를 실행하여 apk파일을 생성 (구글 개발자에 올리는 버전이 아닙니다)