用php接收android POST数据

时间:2012-05-05 10:01:08

标签: php android post

我已经搜索了一些答案,但我找不到任何答案。那么如何使用PHP从Android应用程序使用POST接收和保存发送给它的数据。我目前正在使用以下代码,因此PHP将用于存储数据。有关此教程的任何链接也会有所帮助。

public void send(View v)
 {
    // get the message from the message text box
    String msg = msgTextField.getText().toString();  

    // make sure the fields are not empty
    if (msg.length()>0)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://kiwigaming.beastnode.net/upload.php");
     try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("Date", "Unitnumber"));
       nameValuePairs.add(new BasicNameValuePair("Doneornot", msg));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       httpclient.execute(httppost);
       msgTextField.setText(""); // clear text box
     } catch (ClientProtocolException e) {
         // TODO Auto-generated catch block
     } catch (IOException e) {
         // TODO Auto-generated catch block
     }

    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
    }

}

2 个答案:

答案 0 :(得分:3)

超级全球$_POST包含帖子数据。

<?php
     print_r($_POST);
?>

答案 1 :(得分:0)

您需要先替换以下内容获取响应:

httpclient.execute(httppost);

使用:

HttpResponse response = httpclient.execute(httppost);

然后你可以去

Head[] head = response.getHeaders();
for(int i = 0, i < head.length; i++){
    //find header?
}

或获取内容?

String content = response.getEntity.getContent();