In one PHP, android tries to send more than one json format, but only one is received. What should I do?

서성환 :

In one PHP, android tries to send more than one json format, but only one is received. What should I do?

Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    Log.d("GGGG","onResponse start");
                    JSONObject jsonResponse = new JSONObject(response);
                    Log.d("GGGG","test" );
                    JSONArray jsonArray = jsonResponse.getJSONArray("Search");
                    Log.d("GGGG","test1" );
                    JSONArray jsonArray2 = jsonResponse.getJSONArray("Favorite");
                    Log.d("GGGG","test2" );

Log to Logcat.d is output to 'test1' and is not output. I think we didn't get the value from 'getJSONArray ("Favorite").' What should I do?

//-------PHP-------//

$userID =$_POST["userId"];
   $keyWord = $_POST["keyWord"];

    //  $keyWord = "na";
    //  $userID ="test";
    //데이터베이스에 값을 넣는부분

      $statement ="SELECT * from music where music_title like '%$keyWord%' or music_singer like '%$keyWord%'";

    //    echo $statement;
    //      exit;
      $result = mysqli_query($con,$statement);
      $resultArray = array();

        while($rows=mysqli_fetch_assoc($result)){
          $resultArray[]=$rows;

        }
      $statement ="SELECT * from favorite_playlist WHERE user_id ='$userID'";

      $result = mysqli_query($con,$statement);
      $resultArray2 = array();
      while($rows=mysqli_fetch_assoc($result)){
        $resultArray2[]=$rows;

      }

      //$response["success"] = true;//success라는 인덱스에 true값을 넣어줌

      header('Content-Type: application/json; charset=utf8');
      $json =  json_encode(array("Search"=>$resultArray), JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE);
      $json2 = json_encode(array("Favorite"=>$resultArray2), JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE);
      //echo json_encode($resultArray);//JSON형식으로 출력
      echo $json;
      echo $json2;

Below is the php result.

--------PHP example Result-----

{
    "Search": [
        {
            "music_id": "1",
            "music_title": "나빠(NAPPA)",
            "music_singer": "Crush",
            "music_img": "https:\/\/wallkpop.com\/wp-content\/uploads\/2019\/08\/%EB%82%98%EB%B9%A0NAPPA-2.jpg"
        }
    ]
}{
    "Favorite": [
        {
            "user_id": "test",
            "music_id": "3",
            "play_time": "2019-09-05 09:35:21"
        },
        {
            "user_id": "test",
            "music_id": "4",
            "play_time": "2019-09-05 09:36:20"
        },
        {
            "user_id": "test",
            "music_id": "5",
            "play_time": "2019-09-05 13:02:34"
        },
        {
            "user_id": "test",
            "music_id": "2",
            "play_time": "2019-09-05 13:02:36"
        }
    ]
}
Iman :

Only one response string (JSON or XML encoded) can be received. You have two options here :

  1. Seperate them into two different Api route
  2. Merge them into one JSON string

In your case I would have gone with the first option.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161527&siteId=1