简体   繁体   中英

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. 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:

        $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:

     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. The first JSON it's working I receive the values and I can save them in the "cod_client", "cnp", "kg" and "inaltime".

Thanks in advance for your help.

If you do multiple echo of jsons then it will become invalid 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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