Java Android应用程序保存从PHP脚本接收的JSON数据
我想保存一些数据从服务器发送到我的应用程序使用PHP脚本。 现在,我的问题是,我在应用程序级别收到了数据,但无法正确保存,因为我将其视为空白“”。 我附加PHP脚本中的代码:Java Android应用程序保存从PHP脚本接收的JSON数据,java,php,android,Java,Php,Android,我想保存一些数据从服务器发送到我的应用程序使用PHP脚本。 现在,我的问题是,我在应用程序级别收到了数据,但无法正确保存,因为我将其视为空白“”。 我附加PHP脚本中的代码: $response2 = array(); $response= array(); while($row = mysqli_fetch_array($result2) ) { $response = array("cod_client"=&
$response2 = array();
$response= array();
while($row = mysqli_fetch_array($result2) )
{
$response =
array("cod_client"=>$row[0],"cnp"=>$row[1],"kilograme"=>$row[2],"inaltime"=>$row[3]);
}
while($row2 = mysqli_fetch_array($result3))
{
$response2 = array("fitness"=>$row2[0]);
}
echo json_encode(array("user_data"=>$response));
echo json_encode(array("values"=>$response2));
以下是Android JAVA应用程序代码:
String err=null;
try
{
JSONObject emp=(new JSONObject(result)).getJSONObject("user_data");
JSONObject emp2 =(new JSONObject(result)).getJSONObject(values");
cod_client = emp.getString("cod_client");
cnp = emp.getString("cnp");
kg=emp.getString("kilograme");
inaltime=emp.getString("inaltime");
String fitness=emp2.getString("fitness");
Toast.makeText(ctx,"values of fitness are: "+fitness,Toast.LENGTH_LONG).show();
Toast.makeText(ctx,"id is "+cod_client+"cnp este "+cnp,Toast.LENGTH_LONG).show();
}
catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}
我收到的fitness值为“10”,但我无法将其保存在字符串fitness上。
在它工作的第一个JSON中,我收到了值,我可以将它们保存在“cod_client”、“cnp”、“kg”和“inaltime”中
提前感谢您的帮助。如果您对json进行多次回显,那么它将成为无效的json。将其添加为单个数组中的数组 改变 到
echo json_encode(array("user_data"=>$response));
echo json_encode(array("values"=>$response2));
echo json_encode(array("user_data"=>$response,"values"=>$response2));