通过Android发送数据到PHP

通过Android发送数据到PHP

问题描述:

所以,我正试图通过Android中的Post发送消息给PHP
这里是Android Java功能:

So, i'm trying to send message via Post in Android to PHP here is the Android Java Function:

 //enviando para o backend
private void SendtoPHP(String reg) throws IOException {


    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://bubbledev.com.br/gcm/getdevice.php");

    try{
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("regid", "" + reg ));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);


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

}

PHP:

<?php       
include 'conecta.php';
$device_token  = urldecode($_POST['regid']);    
$sql = "INSERT INTO corposa.deviceandroid"
              . " (id,device_token)"
              . " VALUES (NULL,'$device_token')";
mysqli_query($con,$sql);    

?>

PHP是Just很好,我已经用另一篇文章进行了测试和工作,但是当Android的函数试图$ device_token不recive任何价值和SQL保存与在餐桌上

The PHP is Just fine, I already tested it with a another Post and worked, but when the Android function tries $device_token dont recive any value and the SQL save a "" with the id at the table

除了ResponseHandler之外,我使用的代码类似于你的代码。
它对我有用。

I use this code which is similar to yours, except the ResponseHandler. It works for me.

HttpClient Client = new DefaultHttpClient();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", sID));
nameValuePairs.add(new BasicNameValuePair("etc", sETC));

try {           

    String SetServerString = "";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://your-url.com/script.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    SetServerString = httpclient.execute(httppost, responseHandler);                

}  catch(Exception ex) {
    // failed
}