Android-从java运行php脚本

时间:2014-04-22 14:04:01

标签: java php android urlconnection printstream

我想从我的Android应用程序运行位于Web服务器上的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();
}

在简单的java应用程序中使用时,此代码运行良好,但在Android应用程序中使用时会导致错误。 我使用了清单中的权限:

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

错误:

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 个答案:

答案 0 :(得分:1)

由于Android 3.0(Honeycomb)网络连接无法在主线程上完成。您必须使用AsyncTask或其他并发方法。

阅读此manual page on network connections