안드로이드 개발 질문/답변
(글 수 45,052)
도서관 파싱하는거 하고 있는데요...3G망으로 접속하면 파싱을 전혀 못합니다..ㅠ
혹시 3g 쓸때 머 다른 소스코드라도 입력해야되는걸까요?
package GC.Library;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class GC_Library extends Activity {
ProgressDialog mProgress;
String label;
String href;
String content;
SearchThread SThread;
String url;
// <li><a href="/Reference/Privacy.asp">.*</a></li> 정규식 연습용
private EditText getEditText =null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchmain);
this.getEditText = (EditText) findViewById(R.id.Book);// 검색어를 입력할 에디트 텍스트
Button search_btn = (Button) findViewById(R.id.search_btn);// 검색 버튼
search_btn.setOnClickListener(new View.OnClickListener() // 버튼에 리스너를 추가
// 한다
{
public void onClick(View v) {
//DataManager.getInstance().bookItem.clear();//어레이 리스트 클리어
Log.d("getEditText",getEditText.getText().toString());
HandleListView();
}
});
}
protected void onResume(){
super.onResume();
}
//파싱 쓰래드
class SearchThread extends Thread {
String GetUrl; //주소값을 담을 변수
SearchThread(String geturl) {
GetUrl = geturl;
}
public void run() {
try {
URL sUrl = new URL(GetUrl);
Log.d("StartURL", sUrl.toString());
InputStream is = sUrl.openStream();
Source source = new Source(new InputStreamReader(is, "euc-kr"));// 인코딩
source.fullSequentialParse();// 인터넷페이지 풀소스를 가지고 오는 함수
content = source.getTextExtractor().toString(); // content에 페이지
// 소스 담는다
content = new String(content.getBytes(source.getEncoding()),
"euc-kr");// 인코딩함
List<Element> atags = source.getAllElements(HTMLElementName.A);//A태그값을 담는다
for (int i = 1; i < atags.size(); i++) {
Element e = (Element) atags.get(i);
href = e.getAttributeValue("href");
label = e.getContent().getTextExtractor().toString();
if ((href != null && label != null && href != "" && label !="")&&!(label.equals("1"))) {
if (label.equals("선택")) {// 예외처리
label = "";
}
href=href.replace("JavaScript:moveto('/", "");
href=href.replace("')","");
DataManager.getInstance().href.add(href);
Log.d("href",DataManager.getInstance().href.get(i));
DataManager.getInstance().bookItem.add(label);
Log.d("bookItem["+i+"]", "Redgolem"+DataManager.getInstance().bookItem
.get(i));
}
}
} catch (Exception e) {
}
mAfterDown.sendEmptyMessage(0);
}
}
//리스트뷰 전환 제어
private void HandleListView(){
if(!validate()){
return;
}
AddressManager manager =new AddressManager();
try{
HandleArray();//arraylist 값이 1이상일 경우 handleArray() 작동
//바로 검색이 안되는 버그로 인한 어레이값 추가
DataManager.getInstance().bookItem.add("Make By bench87");
DataManager.getInstance().href.add("Make By bench87");
String url = getEditText.getText().toString();
url = URLEncoder.encode(url, "EUC-KR");
manager.setKeyword(url);
}catch(UnsupportedEncodingException e){}
mProgress = ProgressDialog.show(GC_Library.this,
"wait", "Downloading...");
Log.d("getURL",manager.getKeyword());
SThread = new SearchThread(manager.getKeyword());// 검색어가 들어 가서 실행
SThread.start();
}
//EditText값이 널값을 가질때 제어하는 메서드
private boolean validate(){
boolean valid = true;
StringBuilder validationText = new StringBuilder();
if((this.getEditText.getText()==null)||this.getEditText.getText().toString().endsWith("")){
validationText.append("검색어를 입력해주세요");
valid = false;
}
if(!valid){//EditText값이 널값을 가지면 다이얼로그를 실행
new AlertDialog.Builder(this).setTitle("잠깐!!").setMessage(validationText.toString())
.setPositiveButton("Continue",new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int arg1){
//do nothing, show alert is enough
}
}).show();
validationText=null;
}
return valid;
}
// 이벤트 핸들러
Handler mAfterDown = new Handler() {
public void handleMessage(Message msg) {
mProgress.dismiss();// progress exit
Intent intent = new Intent("BookList");
startActivity(intent);//북리스트로 이동
}
};
//다시 검색했을시 어레이리스트 클리어를 위한 메서드
private void HandleArray()
{
if(DataManager.getInstance().bookItem.size()>1)
{
DataManager.getInstance().bookItem.clear();
DataManager.getInstance().href.clear();
}
}
}