PHP不会从Android Post值返回任何内容

时间:2014-03-04 13:22:16

标签: php android

我正在使用网络服务将用户信息发送到 MySQL PHP 在我尝试回显这些值时没有返回发布的值,并告诉我:< / p>

Notice: Undefined index: user_name in C:\xampp\htdocs\webservice\sign_up.php on line 5

我的代码:

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("http://10.0.2.2/webservice/sign_up.php");
            ArrayList<NameValuePair> post = new ArrayList<NameValuePair>(3);
            post.add(new BasicNameValuePair("user_name", name));
            post.add(new BasicNameValuePair("user_email", email));
            post.add(new BasicNameValuePair("user_password", password));
            request.setEntity(new UrlEncodedFormEntity(post));
            HttpResponse response = client.execute(request);

            int status = response.getStatusLine().getStatusCode();  
            Log.i("data posted, status = " , Integer.toString(status)); 

        } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
        }  

        return null;


    }
}

PHP

<?php
$user = $_POST["user_name"];
echo $user;
?>

2 个答案:

答案 0 :(得分:1)

将字符串从android客户端传递到PHP的示例代码

Android客户端部分

以下代码将使您的Android应用与网页对话。

注意:您还需要在android.manifest文件中启用对Internet的使用权限。

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

Android应用程序代码

public void postData(String toPost) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.URL.com/yourpage.php");

//This is the data to send
String MyName = 'adil'; //any data to send

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);

//This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}

}//end postData()

PHP代码

<?php

echo $_POST["action"];

?>

答案 1 :(得分:0)

您可以执行以下操作之一:

  • 的print_r($ _ POST);
  • 使用像fiddler这样的程序来捕获流量,并查看是否发送了POST请求