我開發一個機器人應用HTTP數據發送到其被保持在虛擬機的後端服務器。無法發送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 [email protected] that was originally added here
07-18 17:35:05.899: E/WindowManager(28043): android.view.WindowLeaked: Activity android.reader.Reader has leaked window [email protected] 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)
你是否在清單文件中包含了INTERNET權限? – adatapost 2012-07-18 08:25:51
你在日誌中看到什麼嗎?你是否設置了正確的權限? – Tomer 2012-07-18 08:26:24
你的意思是logcat,沒有任何顯示,我不知道爲什麼,INTERNET權限?我沒有添加它,因爲我是Android應用程序的初學者,我曾經認爲它可以按照需要訪問互聯網。首先讓我試試看,謝謝 – Conrad 2012-07-18 08:29:39