简体   繁体   English

Android应用程序从PHP脚本保存JSON数据

[英]Android app save data from JSON receieved from a PHP script

I wanna save some data from a server that send it to my app using a PHP script. 我想从服务器保存一些数据,然后使用PHP脚本将其发送到我的应用程序。 Now, my problem is that I received the data at the app level but i can't save it correctly because I see it as blank "". 现在,我的问题是我收到了应用程序级别的数据,但是由于看到的是空白“”,所以无法正确保存。 I attach my code from the PHP script: 我从PHP脚本附加了我的代码:

        $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));

and here is the Android JAVA app code: 这是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();
        }

I receive the value of fitness as "10" but i can't save it on the string fitness. 我收到的适应性值为“ 10”,但无法保存为字符串适应性。 The first JSON it's working I receive the values and I can save them in the "cod_client", "cnp", "kg" and "inaltime". 工作的第一个JSON我收到了值,可以将它们保存在“ cod_client”,“ cnp”,“ kg”和“ inaltime”中。

Thanks in advance for your help. 在此先感谢您的帮助。

If you do multiple echo of jsons then it will become invalid json. 如果您对json进行多次回显,那么它将成为无效的json。 add it as array in single array change 将其添加为单个数组更改中的数组

echo json_encode(array("user_data"=>$response));

echo json_encode(array("values"=>$response2));

To

echo json_encode(array("user_data"=>$response,"values"=>$response2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM