简体   繁体   中英

Client-Server-Communication with php and android

I'm trying to learn client-server-communication with Android and php with this tutorial:

http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89

So I was successful to make the App (exactly the one in the tutorial) and the php-script works, too (I have tested it with the chrome-extention "Advanced Rest Client").

But when I'm running the app and clicking on the button to send the data, the text in the TextView (I setted it to "hello world" to test it) dissappears and nothing else happens. The TextView stays clear.

In the manifest-file I included the

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

before the application-tag.

What can I do now?


So I putted out the exeptions and got:

10-02 11:56:41.816  13275-13275/? V/Helloagain﹕ android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:410)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:241)
            at java.net.InetAddress.getAllByName(InetAddress.java:219)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
            at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
            at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
            at .MainActivity.GetText(MainActivity.java:99)
            at .onClick(MainActivity.java:54)
            at android.view.View.performClick(View.java:4421)
            at android.view.View$PerformClick.run(View.java:17904)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5214)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
10-02 11:56:41.826  13275-13275/? V/Helloagain﹕ java.lang.NullPointerException
            at .MainActivity.GetText(MainActivity.java:134)
            at .MainActivity$1.onClick(MainActivity.java:54)
            at android.view.View.performClick(View.java:4421)
            at android.view.View$PerformClick.run(View.java:17904)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5214)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
  1. Android by default doesn't have enabled accessing internet on the main thread, so you have 2 solutions:

      1. Create new thread that will access internet
      1. Add this lines before accessing internet (you can use google for more info on this)

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM