drkwpgrdb092239314
drkwpgrdb092239314
2014-04-22 14:04

Android-从java运行php脚本

已采纳

I want to run a php script located on a web server from my android app. this is the code

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();
}

This code is running well when used inside a simple java application, but it results error when used inside android app. I used the permission inside manifest:

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

Errors:

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条回答

  • dongwen7730 dongwen7730 7年前

    Since Android 3.0 (Honeycomb) network connections can't be done on main thread. You have to use AsyncTask or other methods of concurrency.

    Read this manual page on network connections.

    点赞 评论 复制链接分享