Client-server-connection with php and android

I am trying to learn client-server communication with Android and php using this guide:

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

So, I managed to make an application (exactly the same in the tutorial), and the php script works (I tested it with the Chrome extension "Advanced Rest Client").

But when I launch the application and press the button to send the data, the text in the TextView (I set it to "hello world" to check it) disappears and nothing else happens. TextView remains clear.

I included in the manifest file

<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 posted the exceptions 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) 
+5
source share
1 answer
  • Android by default does not have access to the Internet in the main thread, so you have 2 solutions:

      • Create a new stream that will have access to the Internet
      1. Add these lines before accessing the Internet (you can use Google to get more information about this)

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

0
source

Source: https://habr.com/ru/post/1232779/


All Articles