通过Android发送数据到PHP [英] Send Data Via Post Android to PHP

查看:220
本文介绍了通过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
}

这篇关于通过Android发送数据到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆