0

I am developing an android app to send HTTP data to the backend server which is holding in a virtual machine.

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();
}

And below is the php code:

<?php
$file = "C:/wamp/www/application/file.txt";
$data = "device:".$_POST['device']." person:".$_POST['person'];
file_put_contents($file, $data);
?>

However, file cannot be created, as I cannot see the output, so I cannot find out whether the app can successfully connect to the backend server or not. Both my mobile device and the server machine are in the same wifi network, so by typing the virtual address, it should be able for my app to connect to the server, isn't it?

Below is the error message in the 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)

messages show in logcat after I modify the app:

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)
Conrad
  • 933
  • 4
  • 19
  • 34
  • 1
    Had you included INTERNET permission in manifest file? – KV Prajapati Jul 18 '12 at 08:25
  • Do you see anything in the log? Did you set the right permissions? – Tomer Jul 18 '12 at 08:26
  • you mean the logcat, nothing being shown, I don't know why, INTERNET Permission? I have not added it since I am a beginner of android app, I used to think it can access the internet just as it wants. Let me have a try first, thank you – Conrad Jul 18 '12 at 08:29
  • which API level you are using? Any error in `Logcat`? – Mohsin Naeem Jul 18 '12 at 08:31
  • I have just reconnect my device to the desktop and try again, and when I try to send the data, error message occur in the logcat, I will post it above – Conrad Jul 18 '12 at 08:38
  • r u testing this in device or emulator? – Jayabal Jul 18 '12 at 08:47
  • I am testing it in a galaxy nexus, which is running in Android 4.0.4, and the target API of my application is API15 – Conrad Jul 18 '12 at 08:50

1 Answers1

3

First of all, Your HtttpPost request URL..

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

Should be something like,

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

Now, second, check your application's manifest file for permission

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

Third one,

Put your complete Web-Request code in Background Thread or AsyncTask's doInBackGround() not in Application's MainUI Thread.

After finally, Check the Response from your Http request, what you are getting..

Also if possible debug your code of Web-Request, So you can find whats going wrong..

Solution:

From your Logcat errors we conclude you are trying to Network Related Operation on your Activity's MainUI Thread. So use AsyncTask for this operation as I suggest you in my third step.

The third step of my answer.

Put your all HttpRequest code in AsyncYAsk's doInBackGround().

user370305
  • 108,599
  • 23
  • 164
  • 151
  • so does you mean I have to create an AsyncTAsk class to handle the request? – Conrad Jul 18 '12 at 08:44
  • Yes. Because you are blocking Application's MainUI Thread on your Web-Request. – user370305 Jul 18 '12 at 08:46
  • actually, I have found this site, http://pcfandroid.wordpress.com/2011/07/14/http-post-with-asynctask-android-tutorial/ – Conrad Jul 18 '12 at 09:01
  • Look at SO question http://stackoverflow.com/questions/3505930/make-an-http-request-with-android and this tutorial http://www.vogella.com/articles/AndroidPerformance/article.html – user370305 Jul 18 '12 at 09:03
  • I have set up an AsyncTask class, but problem still exist, some messages occur in the logcat which I have no idea what is happening – Conrad Jul 18 '12 at 09:37
  • And actually, my app is also related to NFC tag, but I dont know why, if my app is launch by sensing the tag, it requires me to click on the button I set before it sends the data, but if I have launched the program first and then snese the tag, it will send the data without i have push on the tag. I have created an AsyncTask object in my Activity class – Conrad Jul 18 '12 at 09:41