Phonegap中的JSON解码

时间:2015-03-16 14:44:36

标签: javascript json cordova push-notification google-cloud-messaging

我希望我的应用程序中接收的JSON数据被解码并存储到MySQL数据库中。

这是使用Phonegap制作的Android应用程序。

以下是应用程序代码:

function onNotification(e) {
                $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

                switch( e.event )
                {
                    case 'registered':
                    if ( e.regid.length > 0 )
                    {
                        $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
                        console.log("regID = " + e.regid);
                        $("#pp").click(function(){
    tokenHandler(e.regid);
});
                    }
                    break;

                    case 'message':
                        if (e.foreground)
                        {
        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
        var soundfile = e.soundname || e.payload.sound;
        var my_media = new Media("/android_asset/www/"+ soundfile);
        my_media.play();
                        }
                        else
                        {   
                        if (e.coldstart)
    $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                        else
    $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                        }
    $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');

                        //android only
                        $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                        //amazon-fireos only
                        $("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</li>');
                    break;

                    case 'error':
                        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                    break;

                    default:
                        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                    break;
                }
            }

e.payload.message包含要解码的消息。 这是服务器端代码:

 <?php

    class GCMPushMessage {
        var $url = 'https://android.googleapis.com/gcm/send';
        var $serverApiKey = "xxxxxxxxxxxxxxx";
        var $devices = 0;


        function GCMPushMessage(){
            //$this->serverApiKey = $apiKeyIn;
        }


        function setDevices($deviceIds){

            if(is_array($deviceIds)){
                $this->devices = $deviceIds;
            } else 
    {
                $this->devices = array($deviceIds);
            }

        }


        /*
            Send the message to the device
            @param $message The message to send
            @param $data Array of data to accompany the message
        */
        function send($message, $data = false){
            //echo $this->serverApiKey.$this->devices[0];
            if(!is_array($this->devices) || count($this->devices) == 0){
                $this->error("No devices set");
            }

            if(strlen($this->serverApiKey) < 8){
                $this->error("Server API Key not set");
            }

            $fields = array(
                'registration_ids'  => $this->devices,
                'data'              => array( "message" => $message ),
            );

            if(is_array($data)){
                foreach ($data as $key => $value) {
                    $fields['data'][$key] = $value;
            }
            }
            $headers = array( 
                'Authorization: key=' . $this->serverApiKey,
                'Content-Type: application/json'
            );
            // Open connection
            $ch = curl_init();

            // Set the url, number of POST vars, POST data
            curl_setopt( $ch, CURLOPT_URL, $this->url );

            curl_setopt( $ch, CURLOPT_POST, true );
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

            curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

            // Avoids problem with https certificate
            curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);

            // Execute post
            $result = curl_exec($ch);

            // Close connection
            curl_close($ch);

            return $result;
        }


        function error($msg){
            echo "Android send notification failed with error:";
            echo "\t" . $msg;
            exit(1);
        }
    }

    $query="select * from prescription where id=".$id;
    $results1=mysql_query($query) or die(mysql_error());
    $rs1=mysql_fetch_assoc($results1);
    $mid=$rs1['med_id'];
    $query="select * from medicine where mid=".$rs1['med_id'];
    $results2=mysql_query($query) or die(mysql_error());
    if(isset($_POST['submit']))
    {
        $diagnosis=$_POST['diagnosis'];
        $instructions=$_POST['instructions'];
        $query=mysql_query("update prescription set diagnosis='".$diagnosis."' , instructions='".$instructions."' where id=".$id);
        $rc=mysql_num_rows($results2);
        $i=1;
        $query="delete from medicine where mid=".$mid;
        $results3=mysql_query($query);
        while($i<=$rc)
        {
            $medicine=' ';
            $tm1=$tm2=$tm3=0;
            $medicine=$_POST['old_'.$i];
            $tm='';
            if(isset($_POST['old_1_'.$i]))
                {$tm1=1;}
            if(isset($_POST['old_2_'.$i]))
                {$tm2=1;}
            if(isset($_POST['old_3_'.$i]))
                {$tm3=1;}
            $dosage=$_POST['old_dosage_'.$i];
            $str="insert into medicine values('$mid','$dosage','$medicine','$tm1','$tm2','$tm3')";
            $res=@mysql_query($str)or die(mysql_error());
            $i++;
        }
        $nf=$_POST['nf'];
        $i=1;
        while($i<=$nf)
            {
            $medicine=' ';
            $tm1=$tm2=$tm3=0;
            $medicine=$_POST['medicine_'.$i];
            $tm='';
            if(isset($_POST['tm_1_'.$i]))
                {$tm1=1;}
            if(isset($_POST['tm_2_'.$i]))
                {$tm2=1;}
            if(isset($_POST['tm_3_'.$i]))
                {$tm3=1;}
            $dosage=$_POST['dosage_'.$i];
            $str="insert into medicine values('$mid','$dosage','$medicine','$tm1','$tm2','$tm3')";
            $res=@mysql_query($str)or die(mysql_error());
            $i++;
        }
    $gcpm = new GCMPushMessage();
    $sql=mysql_query("select token from device where id=".$id);
    $rs=mysql_fetch_assoc($sql);
    $gcpm->setDevices($rs['token']);
    $query1=mysql_query("select * from medicine,prescription where med_id=mid and id=".$id);
    while($rs1=mysql_fetch_assoc($query1))
    {
    $rows[]['tm_1']=$rs['tm_1'];
    $rows[]['tm_2']=$rs['tm_2'];
    $rows[]['tm_2']=$rs['tm_3'];
    $rows[]['diagnosis']=$rs['diagnosis'];
    $rows[]['instructions']=$rs['instructions'];
    $rows[]['medicine_name']=$rs['medicine_name'];
    $rows[]['dosage']=$rs['dosage'];
    }
    $response = $gcpm->send($message, $rows);
    $retval = mysql_query( $sql );
    }

我试图解析JSON但失败了。有什么想法吗?

0 个答案:

没有答案