2016-05-03 16 views
0

GCMからJSON配列としてレスポンスを得るのに手伝ってくれる人はいますか?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

を、あなたが取得しているどのようなエラー? – Amy

+0

あなたはログから得たあなたの応答を書くことができますか? – Masum

答えて

0

まず

あなたはJSONArrayが、あなたですJSONObjectで解析してみてください。適切な応答を取得するには、この

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

によって

変更ライン​​3210セカンド

変更しますサーバー側のコード。キーusernametitleはあなたの応答に含まれていません。

+0

あなたは私の一日を保存してくれてありがとう:-D –

+0

@SurajKumar、もしそれが役に立つなら、Upvoteに感謝します:) –

+0

ここで2つの答えはどのように私は両方の答えをupvoteすることができますか?申し訳ありませんが、私は新しいスタックオーバーフローです –

1

あなたのlogcatレスポンスは、[{"id": "suraj854"、 "0": "Suraj"、 "1": "1234567890"、 "2": "42.6640983"、 "3": "56.9954983" }] あなたのフォーマットが間違っています、あなたのPHPスクリプトを更新してください。 この行をPHPスクリプトから更新するarray_push($ userdata、array( 'id' => $ users ["username"]、$ users ["first_name"]、$ users ["phone"]、$ users ["longitude_current" ]、$ users ["latitude_current"]));

にarray_push($ユーザデータ、配列( 'ユーザ名' => $ユーザー[ "ユーザー名"]、 'ファーストネーム' => $ユーザー[ "FIRST_NAME"]、 '電話' => $ユーザー[」 "latitude_current" => $ users ["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()); 

         } 




        } 
+0

ありがとうございました –

関連する問題