任何人都可以帮我从GCM获得JSON数组的响应吗?
以下是我收到的错误回复。任何人都可以帮我吗?
我正在创建一个出租车预订应用程序。我想收集包括位置在内的所有驱动程序数据并通过GCM推送它,这样我就能获得每个驱动程序的实时位置。
这是我的错误。
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)
这是我的代码:
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 \
}
这就是我解析JSON数组的方式:
//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));
}
答案 0 :(得分:1)
从你的logcat响应是[{“id”:“suraj854”,“0”:“Suraj”,“1”:“1234567890”,“2”:“42.6640983”,“3”:“56.9954983”} ] 您的格式错误,请更新您的php脚本。 从php脚本更新此行 array_push($ userdata,array('id'=&gt; $ users [“username”],$ users [“first_name”],$ users [“phone”],$ users [“ longitude_current “],$用户[” latitude_current“])); 强>
要 的 array_push($用户数据,阵列( '用户名'=&GT; $用户[ “用户名”], '姓名'=&GT; $用户[ “如first_name”], '电话'=&GT; $用户[ “电话” ], 'longitude_current'=&GT; $用户[ “longitude_current”], 'latitude_current'=&GT; $用户[ “latitude_current”])); 强>
根据您的要求更改密钥。更新以下json解析。
按如下方式更新解析代码:
JSONArray jarr = new JSONArray(message);
if (jaar != null && 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() );
}
}
答案 1 :(得分:0)
首先
您是JSONArray但是您尝试通过JSONObject进行解析。
通过此
更改行JSONObject jobj = new JSONObject(message);
JSONObject jobj = new JSONArray(message).getJSONObject(POSITION);
第二
更改服务器端代码以获得正确的响应。您正在拨打关键username
,title
,而不是您的回复。