Suraj Kumar Suraj Kumar - 6 months ago 75
PHP Question

How to get a JSON array response from GCM

Can anyone help me to get a response as a JSON array from GCM?

Here is the incorrect response I am getting. Can anyone help me with it?

I am creating a cab booking app. I want to collect all driver data including locations and push it through GCM so I can get real-time locations of every driver.

Here is my error.

E/response: [{"id":"suraj854","0":"Suraj","1":"1234567890","2":"42.6640983","3":"56.9954983"}]
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: org.json.JSONException: Value [{"id":"suraj854","0":"Suraj","1":"1234567890","2":"42.6640983","3":"56.9954983"}] of type org.json.JSONArray cannot be converted to JSONObject
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at elegantswebapps.cabdriver.services.GCMPushReceiverService$override.onMessageReceived(GCMPushReceiverService.java:68)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at elegantswebapps.cabdriver.services.GCMPushReceiverService$override.access$dispatch(GCMPushReceiverService.java)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at elegantswebapps.cabdriver.services.GCMPushReceiverService.onMessageReceived(GCMPushReceiverService.java:0)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-03 16:18:17.228 8262-8325/elegantswebapps.cabdriver W/System.err: at java.lang.Thread.run(Thread.java:818)


Here is my code:

public function fetchall_user_online_drivers_() {
$smt = $this->conn->prepare("select * from driver where active_status=1");
$smt->execute();
$userdata = array();
$registration_ids = array();
while ($users = $smt->fetch()) {
//array_push($userdata,$users["username"],$users["first_name"],$users["longitude_current"],$users["latitude_current"]);
array_push($registration_ids, $users['gcm_id_token']);
array_push($userdata,array('id'=>$users["username"],$users["first_name"],$users["phone"],$users["longitude_current"],$users["latitude_current"]));
}

$data = array("message" => json_encode($userdata));

$this->realTimeAllDriverLocations($registration_ids, $data);
}

public function realTimeAllDriverLocations($gcm_id, $userdata) {
//Creating a new array fileds and adding the msg array and registration token array here
$fields = array(
'registration_ids' => $gcm_id,
'data' => $userdata
);

//Adding the api key in one more array header
$headers = array(
'Authorization: key= AIzaSyAlVrqgGDDZVM1G9_qoyEl1OinRFto8WhY',
'Content-Type: application/json'
);

//Using curl to perform http request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

//Getting the result
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);

//Decoding json from result
$res = json_decode($result);

//Getting value from success
$flag = $res->success;

//if success is 1 means message is sent \
}


And this is how I am parsing the JSON array:

//This method will be called on every new message received
@Override
public void onMessageReceived(String from, Bundle bundle) {
//Getting the message from the bundle
if (!bundle.isEmpty()) {
data = bundle;
String message = bundle.getString("message");
try {
Log.e("response", bundle.getString("message"));
JSONObject jobj = new JSONObject(message);
Object obj = jobj.get("");
if (obj instanceof JSONArray) {
final JSONArray jarr = (JSONArray) obj;
if (jarr.length() > 0) {
for (int i = 0; i < jarr.length(); i++) {
JSONObject jsonObject = jarr.getJSONObject(i);
All_UserMapDetail person = new All_UserMapDetail();
if (!jsonObject.isNull("username")) {
person.setName(jsonObject.getString("username"));
}
if (!jsonObject.isNull("title")) {
person.setPhone(jsonObject.getString("title"));
Log.e("hgcghcghc",person.getName() );
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
sendNotification(data.getString(FIRST_NAME_DRIVER_KEY));
}

Answer

First

You are a JSONArray but you try to parse by JSONObject.

Change the line JSONObject jobj = new JSONObject(message); by this

JSONObject jobj = new JSONArray(message).getJSONObject(POSITION);

Second

Change you server side code to get proper response . You are calling key username , title which is not are not in your response .

Comments