2016-07-01 55 views
1

Webサーバー上のMySQLデータベースからデータを取得しようとしています。 私の目標は、すでにMySQLデータベース上にあるロケーションデータの配列を取得することです。 私はサーバーからこのデータの配列を取得していますが、jsonResponse.getJSONArrayからJSONArrayオブジェクトに変換できません。jsonResponse.getJSONArrayが機能しない

private void setUpMap() { 
    Response.Listener<String> responseListener=new Response.Listener<String>(){ 

     @Override 
     public void onResponse(String s) { 
      try { 
       JSONObject jsonResponse=new JSONObject(s); 
       boolean success=jsonResponse.getBoolean("success"); 
       if(success){    //this is true, and also all the data is arrived 
        JSONArray jsonArray = jsonResponse.getJSONArray("data"); //after this line it doesnt work,it doesn't enter in for loop 
        JSONObject jsonObject; 
        for(int i=0;i<jsonArray.length();i++){ 
        jsonObject=jsonArray.getJSONObject(i); 
        String mac=jsonObject.getString("mac"); 
        String android_id=jsonObject.getString("android_id"); 
        Double latitude=jsonObject.getDouble("latitude"); 
        Double longitude=jsonObject.getDouble("longitude"); 
        if(!isMarkerOnArray(markerCollection,latitude,longitude)) 
        markerCollection.add(mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)))); 
        } 

       } 
       else{ 
        AlertDialog.Builder builder=new AlertDialog.Builder(MapsActivity.this); 
        builder.setMessage("Downloading position failed") 
          .setNegativeButton("retry",null) 
          .create() 
          .show(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    DownloadPosition downloadPosition=new DownloadPosition(responseListener); 
    RequestQueue queue= Volley.newRequestQueue(MapsActivity.this); 
    queue.add(downloadPosition); 


} 

debug

これは私のPHPスクリプトです

`<?php 
$con=mysqli_connect("","","",""); 
$sql="SELECT * FROM positions"; 
$result=mysqli_query($con,$sql); 
$response["data"]=array(); 
$data=array(); 
$data["success"]=false; 
$i=0; 
while($rs = mysqli_fetch_assoc($result)){ 
    $data["success"]=true; 
    $data[$i] = array('id'=>$rs['id'],'mac'=>$rs['mac'], 'android_id'=>$rs['android_id'], 'latitude'=> $rs['latitude'],'longitude'=> $rs['longitude']); 
    $i++; 
} 
echo json_encode($data); 
mysqli_close($con); 
?>` 

ので、私はjsonResponse.getJSONArrayからJSONArrayオブジェクトをどのように取得することができている私の質問()?

編集: this is json

+0

何あなたのJSONは見えますか? – Apoorv

+0

変換できないということはどういう意味ですか?どのラインが問題を引き起こしていますか?そして、まさに問題は何ですか? @Apoorvが尋ねるように、あなたのJSONを追加することも便利です –

+0

印刷されているJSONを投稿できますか?あなたのPHPスクリプトがキー 'data'を使って配列を出力しているように見えません。 –

答えて

0

あなたはブール値とJsonObjectを含む応答でJsonObjectを取得します。

だから、あなたのPHPコードの変更、

$result=array(); 
while($rs = mysqli_fetch_assoc($result)){ 
     $result[$i] = array('id'=>$rs['id'],'mac'=>$rs['mac'], 'android_id'=>$rs['android_id'], 'latitude'=> $rs['latitude'],'longitude'=> $rs['longitude']); 
    $i++; 
} 

$data['data']=$result; 
echo json_encode($data); 
+0

ありがとうございました。 私はこれを好きでしたhttp://pastie.org/10897525 – giusha9