无法将HTTP请求发送到后端服务器

时间:2012-07-18 08:24:35

标签: php android

我正在开发一个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)

1 个答案:

答案 0 :(得分:3)

首先,您的HtttpPost请求网址..

HttpPost post = new HttpPost("192.168.1.150/application/check.php");

应该是这样的,

HttpPost post = new HttpPost("http://192.168.1.150/application/check.php");

现在,第二,检查应用程序的清单文件以获取许可

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

第三个,

将完整的Web-Request代码放在后台线程或AsyncTask的doInBackGround()中,而不是应用程序的 MainUI线程

最后,检查您的Http请求中的响应,你得到了什么..

如果可能的话调试你的Web-Request代码,那么你可以找到什么问题..

<强>解决方案:

根据您的Logcat错误,我们得出结论,您正在尝试在您的活动MainUI Thread网络相关操作。因此,正如我在第三步中所建议的那样,使用AsyncTask进行此操作。

我回答的第三步。

将所有 HttpRequest 代码放入AsyncYAsk's doInBackGround()