2016-05-29 8 views
-1

私はアンドロイドプログラミングの新人です。私は、JSONを使用してアンドロイドでmysqlデータベースからデータを取得しようとしているときに問題が発生しています。これは私の方法です。これは私が受け取るエラーです: :java.lang.Integer型の値60はJSONObjectE/JSON Parser:データの解析エラー[java.lang.Integer型の値60をJSONObjectに変換できません] 60(97)

private void getData(final String mac_Address){ 
    //Creating a string request 
    // Tag used to cancel the request 
    String tag_string_req = "req_monitor"; 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_MONITOR_HEALTH_PARAMETERS, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        JSONObject j = null; 
        try { 

         //Parsing the fetched Json String to JSON Object 
         j = new JSONObject(response); 
         //boolean error = j.getBoolean("error"); 

         //j = new JSONObject().append("userid", new JSONArray(json).getInt(0)); 
         //boolean error = j.getBoolean("error"); 
         //return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); 

         //Storing the Array of JSON String to our JSON Array 
         result = j.getJSONArray("result"); 
         //new JSONObject().append("pulse_rate", new JSONArray(result).getInt(0)); 
         //JSONArray k = null; 
         for(int i=0;i<result.length();i++){ 
          try { 
           //Getting json object 
           JSONObject json = result.getJSONObject(i); 

           pulse_rate.add(json.getInt("pulse_rate")); 
           pulse_oxygen_saturation.add(json.getInt("pulse_oxygen_saturation")); 
           Log.d(TAG, "Fetching pulse rate from mysql: " +pulse_rate+" Fetching pulse rate from mysql: "+pulse_oxygen_saturation); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         //Calling method getMonitoringData to get the students from the JSON Array 
         // getMonitoringData(result); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
         Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+response); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 

      }) 
    { 

     @Override 
     protected Map<String, String> getParams() { 
      // Posting parameters to monitor url 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("mac_Address", mac_Address); 
      return params; 
     } 

    }; 

    //Creating a request queue 
    //RequestQueue requestQueue = Volley.newRequestQueue(this); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req); 

    //Adding request to the queue 
    //requestQueue.add(stringRequest); 

} 

に変換することはできませんこれは、これは私のログに私のJSONレスポンスである私のPHPコード

$query = "SELECT pulse_rate, pulse_oxygen_saturation from health_para"; 
if ($result = mysqli_query($link, $query)) { 

/* fetch associative array */ 
$result1 = array(); 
while ($row = mysqli_fetch_row($result)) { 
    printf ("%s (%s)\n", $row[0], $row[1]); 
array_push($result1,array(
    'pulse_rate'=>$row[0], 
    'pulse_oxygen_saturation'=>$row[1], 
)); 
    //$response[]=$row; 
    //echo json_encode($response); 
    echo json_encode(array('result'=>$result1)); 
} 

/* free result set */ 
mysqli_free_result($result); 
} 

/* close connection */ 
mysqli_close($link); 

    } else { 
// required post params is missing 
$response["error"] = TRUE; 
$response["error_msg"] = "Required parameters mac_address is missing!"; 
echo json_encode($response); 
} 

ですこれは私のログにあります:

{ 
"result": [{ 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "97" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "97" 
}, { 
    "pulse_rate": "59", 
    "pulse_oxygen_saturation": "97" 
}, { 
    "pulse_rate": "59", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "58", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "59", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "59", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "58", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "58", 
    "pulse_oxygen_saturation": "98" 
}, { 
    "pulse_rate": "255", 
    "pulse_oxygen_saturation": "127" 
}, { 
    "pulse_rate": "70", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "69", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "67", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "66", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "62", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "62", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "62", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "61", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "61", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "65", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "65", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "66", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "67", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "66", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "65", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "64", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "255", 
    "pulse_oxygen_saturation": "127" 
}, { 
    "pulse_rate": "72", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "73", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "71", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "65", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "62", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "59", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "255", 
    "pulse_oxygen_saturation": "127" 
}, { 
    "pulse_rate": "66", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "63", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "62", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "61", 
    "pulse_oxygen_saturation": "99" 
}, { 
    "pulse_rate": "60", 
    "pulse_oxygen_saturation": "99" 
}] 
} 
+0

あなたのjsonレスポンスを投稿してください。あなたのjsonの応答とそれを解析する方法にはいくつかの違いがあるようです。 {"result":[{"pulse_rate": "60"、 "pulse_oxygen_saturation": –

+0

} {"result":[{"pulse_rate": "60"、 "pulse_oxygen_saturation": "97"}]} 60(97) { "97"}、{"pulse_rate": "60"、 "pulse_oxygen_saturation": "97"}}} 59(97) – hermeshabib

+0

有効なJSONのようには見えません。正当なJSONレスポンスを投稿してください。 –

答えて

0

これを試してみてください。

pulse_rate.add(json.getString("pulse_rate")); 
          pulse_oxygen_saturation.add(json.getString("pulse_oxygen_saturation")); 
+0

このエラーが発生しました:java.lang.NullPointerException pulse_rate.add(json.getInt( "pulse_rate")); – hermeshabib

関連する問題