안드로이드 개발 질문/답변
(글 수 45,052)
//소스하이라이트를 먼저 등록하니 질문이 아래로 가게 되네요..;; 죄송합니다. 질문은 소스 하단에 적겠습니다.
package com.testappl;
import java.io.*;
import java.net.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class testapplActivity extends Activity {
TextView mResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mResult = (TextView)findViewById(R.id.result);
Button btn = (Button)findViewById(R.id.down);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String xml;
String url = "http://www.naver.com";
xml = DownloadHtml(url);
TextView result = (TextView)findViewById(R.id.result);
result.setText(xml);
}
});
}
String DownloadHtml(String addr) {
StringBuilder html = new StringBuilder();
try {
URL url = new URL("addr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if (conn != null) {
conn.setConnectTimeout(10000);
conn.setUseCaches(false);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
for (;;) {
String line = br.readLine();
if (line == null) break;
html.append(line + '\n');
}
br.close();
}
conn.disconnect();
}
}
catch (Exception ex) {;}
return html.toString();
}
}
△testappl.java (소스코드)
▽ main.xml
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text = "@string/hello" /> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text = "@string/hello" /> </LinearLayout>
단순히 버튼을 클릭하면 웹 페이지의 HTML소스를 보여주는 어플리케이션입니다.
책(김상형 저 안드로이드 프로그래밍 정복)보고 따라했는데 잘 안되네요..
버튼을 클릭하면 초기값(hello)만 사라질 뿐 소스가 출력되지 않습니다.