마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

httpget으로 서버에서 html파일을 읽어오는데요..

0 추천

안녕하세요. 안드로이드 초보입니다.

avd로 돌릴때는 잘 받아오는데요..제폰으로 돌리면 안됩니다, 

소스코드

<Today.java>

package com.example.ystory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class Today {
	public static class m_HttpGet {
		public String executeHttpGet() {
			BufferedReader in = null;
			HttpGet request = new HttpGet();
			String page = "";
			int m_month = 11;
			try {
				HttpClient client = new DefaultHttpClient();

				switch (m_month) {
				case 0:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_0_1.html"
									+ "");
					break;
				case 1:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_1_2.html"
									+ "");
					break;
				case 2:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_2_3.html"
									+ "");
					break;
				case 3:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_3_4.html"
									+ "");
					break;
				case 4:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_4_5.html"
									+ "");
					break;
				case 5:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_5_6.html"
									+ "");
					break;
				case 6:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_6_7.html"
									+ "");
					break;
				case 7:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_7_8.html"
									+ "");
					break;
				case 8:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_8_9.html"
									+ "");
					break;
				case 9:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_9_10.html"
									+ "");
					break;
				case 10:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_10_11.html"
									+ "");
					break;
				case 11:
					request = new HttpGet(
							"http://kitmobile.cafe24.com/20128140/monthly_11_12.html"
									+ "");
					break;

				}
				
				// request.setURI(new URI("http://naver.com"));
				HttpResponse response = client.execute(request);

				/*
				 * response를 통해 데이터가 들어옴 statusCode를 통해 데이터가 정상적으로 들어왓는지 확인함 밑에
				 * Log.i가 실행이 된다면 그건 잘못된것이다.
				 */

				final int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode != HttpStatus.SC_OK) {
					Log.i("MJY", "response statusCode -" + statusCode);
					return null;
				}
				in = new BufferedReader(new InputStreamReader(response
						.getEntity().getContent(), "UTF-8"));

				StringBuffer sb = new StringBuffer("");

				/* StringBuffer는 데이터형의 제한을 받지 않는다. */

				String line = "";

				String NL = System.getProperty("line.separator");

				/*
				 * System.getProperties()는 현재 사용하고 있는 환경 정보를 가져오는 것이다. 즉,
				 * line.separator라는 정보를 NL에 가져오는 것.
				 */

				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				/*
				 * 읽어와서 sb에 집어 넣는다. 예를 들어 sb.append(123); sb.append("abc");이러면
				 * System.out.println(sb); 찍으면 123 abc 하고 뜬다.
				 */

				in.close();

				/*
				 * 사용하고 꼭 닫아줘야된다 안그러면 남으니까 메모리 차지
				 */
				page = sb.toString();
				System.out.println(page);
				Log.i("MJY", page);

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return page;
		}
	}
}

<main.java>

package com.example.ystory;

import com.example.ystory.Today.m_HttpGet;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.graphics.Color;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class main extends Activity implements OnTabChangeListener {
	
	m_HttpGet http = null;
	TextView tv;
	TabHost tabHost;
	private BackPressCloseHandler backPressCloseHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tv = (TextView) findViewById(R.id.textView1);
		http = new m_HttpGet();

		String tmp = http.executeHttpGet();
		tv.setText(tmp);
		Log.i("", tmp);
        }
}

고수님들의 도움이 절실해요 ..ㅠㅠ

개문주 (120 포인트) 님이 2013년 11월 2일 질문

1개의 답변

0 추천

네트워크 통신을 main ui thread에서 수행하신듯 한데요.

아마 테스트 하셨던 애뮬레이터 버젼이 ICS 이전 버젼인 듯 하군요.. 

아래 내용을 참조해서 통신을 다른 thread에서 수행하게 하세요.

http://stackoverflow.com/questions/15653739/network-on-main-thread-exception

사악미소 (65,330 포인트) 님이 2013년 11월 5일 답변
...