我正在尝试使用本教程学习与Android和php的客户端 - 服务器通信:
所以我成功地制作了应用程序(完全是教程中的那个),并且php脚本也可以工作(我已经使用chrome-extention“Advanced Rest Client”测试了它。)
但是当我运行应用程序并单击按钮发送数据时,TextView中的文本(我将其设置为“hello world”进行测试)会消失,并且不会发生任何其他情况。 TextView保持清晰。
在清单文件中,我包含了
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在application-tag之前。
我现在能做什么?
所以我推出了exeptions并得到了:
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)
答案 0 :(得分:0)
默认情况下Android没有在主线程上启用访问互联网,因此您有2个解决方案:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode.setThreadPolicy(策略);