drkwpgrdb092239314 2014-04-22 14:04
浏览 135
已采纳

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 2014-04-22 14:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?