04-02 01:59:44.230: E/SampleHTTP(7504): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
04-02 01:59:44.230: E/SampleHTTP(7504): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
04-02 01:59:44.230: E/SampleHTTP(7504): at java.net.Socket.connect(Socket.java:901)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
04-02 01:59:44.230: E/SampleHTTP(7504): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:736)
04-02 01:59:44.230: E/SampleHTTP(7504): at com.enisystem.SampleHTTP.SampleHTTPActivity.request(SampleHTTPActivity.java:59)
04-02 01:59:44.230: E/SampleHTTP(7504): at com.enisystem.SampleHTTP.SampleHTTPActivity.access$0(SampleHTTPActivity.java:47)
04-02 01:59:44.230: E/SampleHTTP(7504): at com.enisystem.SampleHTTP.SampleHTTPActivity$1.onClick(SampleHTTPActivity.java:39)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.view.View.performClick(View.java:3122)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.view.View$PerformClick.run(View.java:12020)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.os.Handler.handleCallback(Handler.java:587)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.os.Looper.loop(Looper.java:132)
04-02 01:59:44.230: E/SampleHTTP(7504): at android.app.ActivityThread.main(ActivityThread.java:4126)
04-02 01:59:44.230: E/SampleHTTP(7504): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 01:59:44.230: E/SampleHTTP(7504): at java.lang.reflect.Method.invoke(Method.java:491)
04-02 01:59:44.230: E/SampleHTTP(7504): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
04-02 01:59:44.230: E/SampleHTTP(7504): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-02 01:59:44.230: E/SampleHTTP(7504): at dalvik.system.NativeStart.main(Native Method)
보다시피 위와 같이 2.x에서 멀쩡하게 잘 돌아가는 통신관련 소스가 3.x(허니콤)이상에서는 위와 같이 에러가 뜨면서
문제가 발생합니다.
위의 에러를 없애기 위해서 2가지를 해줘야 합니다.
1) 3.X(허니콤)이상에서는 통신(소켓, http)을 통해서 연결을 할때는 메인스레드에서는 안되고 스레드를 또
하나 만들어 꼭 통신연결을 해야되고 UI를 업데이트하기우해서는 핸들러를 꼭 사용해야 된다.
public class SampleHTTPActivity extends Activity {
/** Called when the activity is first created. */
public static String defaultUrl = "http://m.naver.com";
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.enableDefaults();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button requestBtn = (Button) findViewById(R.id.requestBtn);
requestBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// HttpThread 생성 및호출
HttpThread thread = new HttpThread();
//thread.setDaemon(true);
thread.start();
}
});
}
private String request (String urlStr){
StringBuilder output = new StringBuilder();
try{
URL url = new URL(urlStr); // URL 객체생성
Toast.makeText(this,urlStr, Toast.LENGTH_SHORT).show();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn != null){
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
int resCode = conn.getResponseCode(); // 서버에 접속하여 요청
if(resCode == HttpURLConnection.HTTP_OK){
BufferedReader reader = new BufferedReader( // 응답 결과를 읽기위한 스트림 객체 생성
new InputStreamReader(conn.getInputStream()));
String line = null;
while(true){
line = reader.readLine(); // 반복문 안에서 한 주씩 읽어 결과 문자열에 추가
if( line == null){
break;
}
output.append(line + "\n");
}
reader.close();
conn.disconnect();
}
}
} catch(Exception ex){
Toast.makeText(this,"Exception in processing response.", Toast.LENGTH_SHORT).show();
Log.e("SampleHTTP" , "Exception in processing response.", ex);
}
return output.toString();
}
public class HttpThread extends Thread {
public void run(){
// 핸들러 호출
mHandler.sendEmptyMessage(0);
}
}
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == 0)
{
// 여기다가 코딩
//final String defaultUrl = "http://192.168.0.201:7777/login_mob.asp?um_id=admin&um_pwd=1111"; // + ID=eni&password=123;
final EditText input01 = (EditText) findViewById(R.id.input01);
input01.setText(defaultUrl);
final TextView txtMsg = (TextView) findViewById(R.id.txtMsg);
String urlStr = input01.getText().toString(); // URL 문자열 참조
String output = request(urlStr); // request()메소드 호출
txtMsg.setText(output); // 결과물 표시
}
}
};
}
========================================================================================================================================
<?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" >
<EditText
android:id="@+id/input01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter URL String ..."
android:textSize="14dip"
></EditText>
<Button
android:id="@+id/requestBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Request"
android:textSize="14dip"
android:textStyle="bold"
></Button>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff99ccee"
android:textColor="#ff0000ff"
android:textSize="12dip"
></TextView>
</ScrollView>
</LinearLayout>
2) 아래 부분을 꼭 삽입해줘야 합니다.
public void onCreate(Bundle savedInstanceState) {
StrictMode.enableDefaults(); // 이 부분을 넣어줘야 실행도비니다.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
'안드로이드 개발 > 허니콤(3.0)' 카테고리의 다른 글
허니콤(3.x) 이상에서 프로그램 종료하기 (0) | 2012.04.04 |
---|---|
안드로이드 타이틀바 제거 3.x 이상에서 테스트 (0) | 2012.04.03 |
허니콤(3.x) 이상 버젼에서 http통신할때 (꼭 스레드와 핸들러를 사용해야합니다.) 예제포함 (0) | 2012.04.03 |
webview에서 input type="file" 파일 업로드 처리이슈 해결 팁 (1) | 2012.03.30 |