Android Download html

Programming/Android 2013. 8. 20. 16:43 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc.Test130805;

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

import android.app.Activity;
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.TextView;

public class MainActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c21_downhtml);

		Button btn = (Button) findViewById(R.id.down);
		btn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String html;
				DownloadHtmlThread.start();
			}
		});
	}

	// * 자바의 네트워크 클래스 사용
	Thread DownloadHtmlThread = new Thread() {
		public void run() {
			StringBuilder html = new StringBuilder();
			try {
				URL url = new URL("http://www.google.com");
				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) {
				Log.d("Exceoption", ex.toString());
			}
			Message msg = new Message();
			msg.obj = html.toString();
			mHandler.sendMessage(msg);
		};
	};
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			String str = (String)msg.obj; 
			TextView result = (TextView) findViewById(R.id.result);
			result.setText(str);
		};
	};

	// */

	/*
	 * 아파치 클래스 사용 String DownloadHtml(String addr) { HttpGet httpget = new
	 * HttpGet(addr); DefaultHttpClient client = new DefaultHttpClient();
	 * StringBuilder html = new StringBuilder(); try { HttpResponse response =
	 * client.execute(httpget); BufferedReader br = new BufferedReader(new
	 * InputStreamReader(response.getEntity().getContent())); for (;;) { String
	 * line = br.readLine(); if (line == null) break; html.append(line + '\n');
	 * } br.close(); } catch (Exception e) {;} return html.toString(); } //
	 */
}


'Programming > Android' 카테고리의 다른 글

Android 채팅 소스 02  (0) 2013.08.20
Android 채팅 소스 01  (0) 2013.08.20
Andoid 매트로돔  (0) 2013.08.19
Android 시계  (0) 2013.08.19
Andoid 알람 기능으로 라디오 mms 주소로 듣기  (0) 2013.08.16