Both my laptop and my Android device is connected to the same WiFi. and I am trying to send data from my phone to the PHP webservice. But I can't get it work. What is the problem?
IP of my laptop: 192.168.0.10
IP of my phone: 192.168.0.9
I am listening from port80: ie:
HttpPost httppost = new HttpPost("http://192.168.0.10:80");
Following are the code of my PHP file:
<?php
$get = json_encode($_POST["req"]);
// Get data from object
$name = $get->req; // Get name you send
$age = $get->req; // Get age of user
?>
and below are my android code. Which part i'm doing wrongly? Please help!
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.10:80");
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("name", "Jensen");
jsonobj.put("age", "22");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("req", jsonobj.toString()));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Another thing, I have tried sending the data over too. I will need to press a button then the data will be passed. After I press the button, this is what I got (I'm connecting my phone through ADB):