客户端-服务器-与php和android通信



我正在尝试学习与Android和php的客户端-服务器通信:

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

所以我成功地使应用程序(正是教程中的一个)和php-脚本工作,太(我已经测试了它与chrome扩展"高级休息客户端")。

但是当我运行应用程序并点击按钮发送数据时,TextView中的文本(我将其设置为"hello world"来测试它)消失了,没有其他任何事情发生。TextView保持清晰。

在manifest-file中我包含了

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

在application-tag前。

我现在能做什么?


所以我列出了异常,得到:

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默认不允许在主线程上访问互联网,所以你有两个解决方案:

      1. 创建一个可以访问internet的线程
    • 在访问互联网之前添加这些行(您可以使用谷歌获取更多信息)

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

相关内容

  • 没有找到相关文章

最新更新