转换android sms数据库日期时,日期不正确(0000-00-00 00:00:00)

时间:2014-02-09 20:18:01

标签: php android sqlite http-post android-contentresolver

我是android新手。 我有一个android程序,它将用户的sms数据从android数据库发送到带有httpPost请求的mysql数据库,并在服务器端使用PHP

我在三个设备上测试它Sony j与android 4 +,Samsung Galaxy与android 4+和LG设备。

在mysql数据库中来自SonySamsung的短信是真的,没有任何错误,但来自LG的笑声有一个很大的错误,即我的mysql中的所有日期都是0000-00-00 00:00:00数据库中。

android方面的代码:

indexID = cursor.getColumnIndex("_id");
indexAddress = cursor.getColumnIndex("address");
indexBody = cursor.getColumnIndex("body");
indexDate = cursor.getColumnIndex("date");
indexRead = cursor.getColumnIndex("read");
indexStatus = cursor.getColumnIndex("status");
while (cursor.moveToNext()) {
        if (Integer.parseInt(cursor.getString(indexRead)) < 1) {
    read = false;
            }
            else {
                read = true;
            }
            switch (Integer.parseInt(cursor.getString(indexStatus))) {
            case -1:
                status = MessageStatus.None;
                break;
            case 0:
                status = MessageStatus.Completed;
                break;
            case 32:
                status = MessageStatus.Pending;
                break;
            default:
                status = MessageStatus.Faild;
                break;
            }
            long id = cursor.getLong(indexID);

                smses.add(new SMSMessage(id,    cursor.getString(indexAddress).replaceAll("-", ""),
                        cursor.getString(indexBody), new Date(cursor.getLong(indexDate)),
                        type, read, status));

从android发送数据:

SimpleDateFormat mySQLFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(serverAddress);
        String sendStr = "";
        int j = 0;
        try {
            for(int i = 0; i < smses.size() && j < 20; i++) {           
                if(smses.get(i) == null || smses.get(i).ID <= maxId)
                    continue;
                j++;
                sendStr += smses.get(i).getAddress() + "`first`";
                sendStr += smses.get(i).getBody() + "`first`";
                sendStr += mySQLFormat.format(smses.get(i).getDate()) + "`first`";
                sendStr += Integer.toString(smses.get(i).getType().ordinal()) + "`first`";
                if (smses.get(i).isRead()) {
                sendStr +=  "1" + "`first`";
                }
                else {
                    sendStr += "0" + "`first`";
                }
                sendStr += Integer.toString(smses.get(i).getStatus().ordinal()) + "`second`";

                MAXID = smses.get(i).ID;

            }

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("syncType", "smstocount"));
            nameValuePairs.add(new BasicNameValuePair("userID", userID));
            nameValuePairs.add(new BasicNameValuePair("data", sendStr));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse response = httpclient.execute(httppost);

PHP方面:

function smsSyncToCount()
{
global $serverAddress, $DBusername, $DBpassword, $DBname;
$userID = $_POST['userID'];
$data   = $_POST['data'];
$smses = explode('`second`', $data);

$con = mysql_connect($serverAddress, $DBusername, $DBpassword);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
    return false;
}
mysql_select_db($DBname, $con);mysql_query("SET NAMES 'utf8'");
foreach($smses as $item)
{
    list($number, $text, $date, $type, $read, $status) = explode("`first`", $item);

    $counter = 0;

    if(!isset($number) || trim($number)==='')
        continue;

    $result = mysql_query("SELECT * FROM `r_sms` WHERE `user_id` = '".$userID."' AND `number` = '".$number."' AND `date` = '".$date."' AND `type` = '".$type."'");
    while($row = mysql_fetch_array($result))
    {
        $counter++;
    }
    if ($counter < 1)
    {
        mysql_query("INSERT INTO `r_sms` (`user_id`, `number`, `text`, `date`, `type`, `read`, `status`) VALUES ('".$userID."', '".$number."', '".$text."', '".$date."', '".$type."', '".$read."', '".$status."')") or die (mysql_error());
    }

}
mysql_close($con);
echo 'ok sent';
return true;
}

我无法理解错误以及为什么LG设备中的日期为NULL ???

0 个答案:

没有答案