为什么我的PHP Web服务无法从我的Android设备接收数据?

时间:2016-01-19 08:53:09

标签: php android json web-services

我的笔记本电脑和Android设备都连接到同一个WiFi。我正在尝试将数据从手机发送到PHP网络服务。但我无法让它发挥作用。有什么问题?

我的笔记本电脑的IP:192.168.0.10
我的手机IP:192.168.0.9

我正在听port80:ie:
             HttpPost httppost =新的HttpPost(" http://192.168.0.10:80");

以下是我的PHP文件的代码:

<?php
$get = json_encode($_POST["req"]);
// Get data from object

$name = $get->req; // Get name you send
$age = $get->req; // Get age of user
?>

以下是我的Android代码。哪个部分我做错了?请帮忙!

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

另一件事,我也试过发送数据。我需要按一个按钮然后传递数据。按下按钮后,这就是我所得到的(我通过ADB连接手机):

Android Monitor to my phone after I press the button.

0 个答案:

没有答案