试图将数据发布到php但数据未正确读取

时间:2011-11-11 22:33:17

标签: php android

以下是php服务器的代码:

?php


    //from the online tutorial:

    $usr = "bikemap";
      $pwd = "pedalhard";
      $db = "test";
      $host = "localhost";

      $cid = mysql_connect($host,$usr,$pwd);

      if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

      $userID = $_POST['userID'];
      $date = $_POST['date'];
      $time = $_POST['time'];

      $lat = $_POST['lat'];
      $long = $_POST['longi'];
      $alt = $_POST['alt'];
       mysql_select_db("test");
      mysql_query("INSERT INTO gpsdata (userID, date, time, lat, longi, alt) VALUES ('$userID', '$date', '$time', '$lat','$longi','$alt') ") or die(mysql_error()); 

      /*$SQL = " INSERT INTO gpsdata ";
    $SQL = $SQL . " (userID, date, time, lat, longi, alt) VALUES ";
    $SQL = $SQL . " ('$userID', '$date', '$time', '$lat','$longi','$alt') ";
    $result = mysql_query("$SQL");

    if (!$result) {
        echo("ERROR: " . mysql_error() . "\n$SQL\n"); } */

    //echo ("New Link Added\n");



    mysql_close($cid); 
    ?>

从我的Android应用程序发送的数据:

 [userID=Loren, date=today, time=now, lat=bit 1, longi=bit 2, alt=bit 3]

由于某些原因,我的php没有正确读取从Android应用程序(如上所示)发送的数据,但如果我从本地网页发送相同的数据,则发布的数据将按原样进行解析。

public static void sendAccelerationData(String userIDArg, String dateArg, String timeArg,
            String timeStamp, String lat, String longi, String alt)
    {


        //Add data to be send.
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
        nameValuePairs.add(new BasicNameValuePair("userID", userIDArg));
        nameValuePairs.add(new BasicNameValuePair("date",dateArg));
        nameValuePairs.add(new BasicNameValuePair("time",timeArg));
        //nameValuePairs.add(new BasicNameValuePair("timeStamp",timeStamp));

        nameValuePairs.add(new BasicNameValuePair("lat",lat));
        nameValuePairs.add(new BasicNameValuePair("longi",longi));
        nameValuePairs.add(new BasicNameValuePair("alt",alt));

        //this.sendData(nameValuePairs);
        try
        {
            TextLog.addLogStuff("SERV Trying to connect to Server");
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new      
            HttpPost("http://myserver.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            Log.i("ServerConn", response.getStatusLine().toString());
            TextLog.addLogStuff("SERV PostData:  "  +response.getStatusLine().toString());
                //Could do something better with response.
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
            TextLog.addLogStuff("SERV Connection error:  " +e.toString());
        }  
    }

3 个答案:

答案 0 :(得分:0)

我需要查看您在Android中使用的代码才能确定数据的POST,但看起来您的Android应用程序并未以正确的格式发布数据。你应该这样做:Android, Java: HTTP POST Request

答案 1 :(得分:0)

正如@John Watson所说,这是因为你的Android应用程序并没有将数据作为你的本地网页发送。 您可以发布您的Android应用程序代码(我不会帮助它)或将发送的数据记录在其他地方,看看有什么不正确。将它们写入日志文件或通过邮件发送。我建议记录它。

<?php
$post = var_export($_POST, true);
$f= fopen('log', 'a+');
fwrite($f, $post);
fclose($f);

需要正确的文件权限。 然后观看您的日志文件&#39; log&#39;

As Said,这个脚本不用于生产,因为需要检查/转发发送的数据以避免sql注入。

答案 2 :(得分:0)

我认为服务器重定向会弄乱信息。