我正在开发一个 android 应用程序来将 HTTP 数据发送到虚拟机中的后端服务器。
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("192.168.1.150/application/check.php");
params.add(new BasicNameValuePair("device", Build.SERIAL));
params.add(new BasicNameValuePair("person", "A"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
下面是php代码:
<?php
$file = "C:/wamp/www/application/file.txt";
$data = "device:".$_POST['device']." person:".$_POST['person'];
file_put_contents($file, $data);
?>
但是,无法创建文件,因为我看不到输出,所以我无法确定应用程序是否可以成功连接到后端服务器。我的移动设备和服务器机器都在同一个wifi网络中,所以通过输入虚拟地址,我的应用程序应该能够连接到服务器,不是吗?
以下是 logcat 中的错误消息:
07-18 16:36:06.407: W/System.err(24598): android.os.NetworkOnMainThreadException
07-18 16:36:06.415: W/System.err(24598): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-18 16:36:06.415: W/System.err(24598): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-18 16:36:06.415: W/System.err(24598): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-18 16:36:06.415: W/System.err(24598): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-18 16:36:06.415: W/System.err(24598): at java.net.Socket.connect(Socket.java:842)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-18 16:36:06.415: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-18 16:36:06.423: W/System.err(24598): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-18 16:36:06.423: W/System.err(24598): at android.reader.Reader$1.onClick(Reader.java:106)
07-18 16:36:06.423: W/System.err(24598): at android.view.View.performClick(View.java:3511)
07-18 16:36:06.423: W/System.err(24598): at android.view.View$PerformClick.run(View.java:14105)
07-18 16:36:06.423: W/System.err(24598): at android.os.Handler.handleCallback(Handler.java:605)
07-18 16:36:06.423: W/System.err(24598): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 16:36:06.423: W/System.err(24598): at android.os.Looper.loop(Looper.java:137)
07-18 16:36:06.423: W/System.err(24598): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 16:36:06.423: W/System.err(24598): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:36:06.423: W/System.err(24598): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 16:36:06.423: W/System.err(24598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 16:36:06.423: W/System.err(24598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 16:36:06.423: W/System.err(24598): at dalvik.system.NativeStart.main(Native Method)
修改应用程序后,logcat 中显示消息:
07-18 17:35:05.899: E/WindowManager(28043): Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c3630 that was originally added here
07-18 17:35:05.899: E/WindowManager(28043): android.view.WindowLeaked: Activity android.reader.Reader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c3630 that was originally added here
07-18 17:35:05.899: E/WindowManager(28043): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.Window$LocalWindowManager.addView(Window.java:537)
07-18 17:35:05.899: E/WindowManager(28043): at android.app.Dialog.show(Dialog.java:278)
07-18 17:35:05.899: E/WindowManager(28043): at android.reader.Reader$GrabURL.onPreExecute(Reader.java:219)
07-18 17:35:05.899: E/WindowManager(28043): at android.reader.Reader$1.onClick(Reader.java:101)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.View.performClick(View.java:3511)
07-18 17:35:05.899: E/WindowManager(28043): at android.view.View$PerformClick.run(View.java:14105)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Handler.handleCallback(Handler.java:605)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 17:35:05.899: E/WindowManager(28043): at android.os.Looper.loop(Looper.java:137)
07-18 17:35:05.899: E/WindowManager(28043): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 17:35:05.899: E/WindowManager(28043): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:35:05.899: E/WindowManager(28043): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 17:35:05.899: E/WindowManager(28043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 17:35:05.899: E/WindowManager(28043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 17:35:05.899: E/WindowManager(28043): at dalvik.system.NativeStart.main(Native Method)
07-18 17:35:05.899: D/OpenGLRenderer(28043): Flushing caches (mode 1)