JSON malformed exception (Android Kotlin Retrofit PHP MySQL) - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, February 24, 2022

JSON malformed exception (Android Kotlin Retrofit PHP MySQL)

I am developing an app with Kotlin-PHP-MySql.

I will be appreciated if someone has an idea why I get this exception "com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path" when I call createUser function.

I don't get an exception when I call getUser function with Same api.

I checked these two functions in postman. JSONs are formatted properly. Begins with { "key" : "value", "key1" : "value1", .... }

This is my api:

     interface Api {
     ...
     @POST("/api/v1/users/register_user.php")
     suspend fun createUser(
         @Body userDto: UserInfoPreviewDto
     ) : QueryResponse

     @POST("/api/v1/users/get_user.php")
     suspend fun getUser(
         @Body  jsonObject : JsonObject
     ) : UserDto
    }

and my Retrofit instance:

    Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(RamonApiUser::class.java)

and my server-side php code is register_user.php file:

      header("Access-Control-Allow-Origin: *");
      header("Content-Type: application/json; charset=UTF-8");
      header("Access-Control-Allow-Methods: POST");
      header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow- 
            Headers, Authorization, X-Requested-With");
      include_once("../../config/database.php");
      include_once("../../classes/user.php");
      $db = new Database();
      $connection = $db->connect();
      $user = new User($connection);
      $query_response = new QueryResponse();
      if($_SERVER['REQUEST_METHOD'] === "POST") {
        $data = json_decode(file_get_contents("php://input"));
        $user->username =  $data->username;
        $user->password =  $data->password;
        $user->fullname =  $data->fullname;
        $user->email =  $data->email;
        if($user->register()) {
            http_response_code(200);
            $result = array( 
                "status" => "200", 
                "message" => "saved"
            );
            echo json_encode($result);
         }
         else {
            http_response_code(500);
            echo json_encode(array(
                "status" => "500",
                "message" => "Error 500"   
            ));
        }    
      }
      else {
          http_response_code(503);
          echo json_encode(array(
                                 "status" => "503",
                                 "message" => "Error 503"   
                           ));
      }

this my get_user.php file:

    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, 
             Authorization, X-Requested-With");
    include_once("../../config/database.php");
    include_once("../../classes/user.php");
    $db = new Database();
    $connection = $db->connect();
    $user = new User($connection);  
    if($_SERVER['REQUEST_METHOD'] === "POST") {
        $param = json_decode(file_get_contents("php://input"));
        if(!empty($param->id)) {
            $user->id = $param->id;
            $user_data = $user->getById();
            if (!empty($user_data)) {
                http_response_code(200);
                $user_data['isPublicProfile'] = (bool)$user_data['isPublicProfile']; 
                $user_data['isLastOnlineVisible'] = 
                                     (bool)$user_data['isLastOnlineVisible'];
                $user_data['isPhoneNumberVisible'] = 
                                     (bool)$user_data['isPhoneNumberVisible'];
                echo json_encode($user_data);
            }
        } 
     }
     else {
          http_response_code(503);
          echo json_encode(array(
                     "status" => "0",
                     "message" => "503 error"   
       ));
     }

getById() function in user.php :

    public function getById() {
        $sqlQuery = "SELECT * FROM ". $this->table_name ." WHERE id = ? LIMIT 0,1";
        $obj = $this->conn->prepare($sqlQuery);
        $obj->bind_param("i", $this->id);
        $obj->execute();
        $data = $obj->get_result();
        return $data->fetch_assoc();
    }

I have QueryResponse data class in the client-side with the fields status and message. user->register() returns true or false . I think the problem is with my json_encode method. I also tried this: I created a QueryReponse php class with fields status and message and encode this object like this:

    $query_response = new QueryResponse();
    $query_response->status = "200";
    $query_response->message = "registration successful";
    $echo json_encode($query_response);

it didn't help either.



source https://stackoverflow.com/questions/71242583/json-malformed-exception-android-kotlin-retrofit-php-mysql

No comments:

Post a Comment