dsx58940 2016-04-02 20:08
浏览 48
已采纳

无法从php脚本中的android中获取发布数据

I am trying to fetch some user fields from android to php

The $mobile_num is being populated correctly but other variables like $user_name, $latitude, $longitude are still empty.

<?php

include('ConnectDB.php');
$user_name=$_POST['userName'];
$mobile_num=$_POST['mobileNo'];
$latitude=$_POST['latitude'];
$longitude=$_POST['longitude'];
$status=1;

echo $mobile_num;

$sql = "INSERT INTO user (username,mobileNo,latitude,longitude,status ) VALUES (?,?,?,?,?)";
$stmt = $con->prepare($sql);
$stmt>bind_param("sssss",$user_name,$mobile_num,$latitude,$longitude,$status);$result=$stmt->execute(); 


if($result)
    echo "inserted ". $result ." row/rows";
else
    die('Could not connect: ' . $con->error);

mysqli_close($con);

?>

On hard coding the values, the code inserts the data correctly, but the post method is unable to catch values for username, latitude, longitude.

This is my android code:

 @Override
  protected String doInBackground(String... params) {

    try {

        data= URLEncoder.encode("mobileNo","UTF-8")
                 + "="+URLEncoder.encode(mUser.getMobileNo(),"UTF-8");

        data+= URLEncoder.encode("userName","UTF-8")
                + "="+URLEncoder.encode(mUser.getUsername(),"UTF-8");

        data+= URLEncoder.encode("latitude","UTF-8")
                + "="+URLEncoder.encode(String.valueOf(mUser.getLatitude()),"UTF-8");

        data+= URLEncoder.encode("longitude","UTF-8")
                + "="+URLEncoder.encode(String.valueOf(mUser.getLongitude()),"UTF-8");

    } catch (Exception e)
    {
        Toast.makeText(activityContext, "ERROR: "+e, Toast.LENGTH_LONG).show();
    }

    return insertUserData(data);
}


private String insertUserData(String data)
{
    try
    {
        conn = DBConnection.createConnnection(php_file);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(data);
        writer.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        response = reader.readLine();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;
}

What shall I do to resolve this issue?

  • 写回答

1条回答 默认 最新

  • dongwei4096 2016-04-02 20:42
    关注

    when you're creating your data string you need to put an '&' between each of your attributes. Do not url encode the '&'.

     data= URLEncoder.encode("mobileNo","UTF-8")
                 + "="+URLEncoder.encode(mUser.getMobileNo(),"UTF-8");
    
        data+= "&" + URLEncoder.encode("userName","UTF-8")
                + "="+URLEncoder.encode(mUser.getUsername(),"UTF-8");
    
        data+= "&" + URLEncoder.encode("latitude","UTF-8")
    
                + "="+URLEncoder.encode(String.valueOf(mUser.getLatitude()),"UTF-8");
    
        data+= "&" + URLEncoder.encode("longitude","UTF-8")
                + "="+URLEncoder.encode(String.valueOf(mUser.getLongitude()),"UTF-8");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测