2014-04-22 6 views
0

내 안드로이드 애플 리케이션에서 웹 서버에있는 PHP 스크립트를 실행하고 싶습니다. 이 간단한 자바 응용 프로그램 내에서 사용할 때이 코드는 잘 실행되는 코드안드로이드 - 자바에서 PHP 스크립트를 실행

try { 
    URL url = new URL("http://www.xxx.com/add.php");  
    URLConnection con = url.openConnection(); 
    con.setDoOutput(true); 
    PrintStream out = new PrintStream(con.getOutputStream()); 
    con.getInputStream(); 
    System.out.print("asdad "); 
    out.close(); 
} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

이지만, 안드로이드 응용 프로그램 내에서 사용하는 경우이 오류가 발생합니다. 내가 매니페스트 내에서 권한을 사용 :

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

오류 :

안드로이드 3.0 (허니 콤) 네트워크 연결은 주 스레드에서 수행 할 수 없기 때문에
04-22 17:10:37.421: E/AndroidRuntime(4518): FATAL EXCEPTION: main 

04-22 17:10:37.421: E/AndroidRuntime(4518): java.lang.IllegalStateException: Could not execute method of the activity 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.view.View$1.onClick(View.java:3758) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.view.View.performClick(View.java:4377) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.view.View$PerformClick.run(View.java:18031) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.os.Handler.handleCallback(Handler.java:725) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.os.Handler.dispatchMessage(Handler.java:92) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.os.Looper.loop(Looper.java:137) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at 
android.app.ActivityThread.main(ActivityThread.java:5306) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.lang.reflect.Method.invokeNative(Native Method) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.lang.reflect.Method.invoke(Method.java:511) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at dalvik.system.NativeStart.main(Native Method) 

04-22 17:10:37.421: E/AndroidRuntime(4518): Caused by: java.lang.reflect.InvocationTargetException 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.lang.reflect.Method.invokeNative(Native Method) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.lang.reflect.Method.invoke(Method.java:511) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.view.View$1.onClick(View.java:3753) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  ... 11 more 

04-22 17:10:37.421: E/AndroidRuntime(4518): Caused by: android.os.NetworkOnMainThreadException 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at java.net.InetAddress.getAllByName(InetAddress.java:214) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at com.example.tmapp.User.AddToDatabase(User.java:79) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  at com.example.tmapp.Registration.reg(Registration.java:85) 

04-22 17:10:37.421: E/AndroidRuntime(4518):  ... 14 more 
+1

무슨 문제가 있습니까? – shanavascet

+1

오류 스택 추적을 제공 할 수 있습니까? –

+0

@ shanavascet, jinx :) –

답변