0

I have problem with fetching data in Retrofit 2

Error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

How to get values received in JSON file?

Call<BasicResponse> call = apiInterface.saveOrderHeader(String.valueOf(order.getOrderNo()), order.getDate(), order.getTime(), Functions.logedUser.getId(), order.getStreet(), order.getMessage(), Functions.cartList.get(0).getRestaurant_id() );
                call.enqueue(new Callback<BasicResponse>() {
                    @Override
                    public void onResponse( Call<BasicResponse> call, Response<BasicResponse> response) {
                        if (response.isSuccessful()) {
                            BasicResponse basicResponse = response.body();
                            Log.d("Id is", basicResponse.getMessage());
                        }

                    }

                    @Override
                    public void onFailure(Call<BasicResponse> call, Throwable t) {
                        Log.d("Error", "Save header error " + t.getMessage());



                    }
                });

BasicResponse.class looks like this:

@SerializedName("success")
        private String success;
        @SerializedName("message")
        private String message;
    
        public BasicResponse() {
        }
    
        public BasicResponse(String success, String message) {
            this.success = success;
            this.message = message;
        }
...getters and setters

  

PHP file

<?php

header ("Content-Type: application/json; charset= UTF-8");
require_once 'connection.php';

    $orderNo= $_POST['orderNo'];
    $date= $_POST['date'];
    $time= $_POST['time'];
    $userId =  $_POST['userId'];
    $deliveryAddress = $_POST['deliveryAddress'];
    $message = $_POST['message'];
    $restaurantId = $_POST['restaurantId'];


    mysqli_query ($conn,"SET NAMES UTF8");
    
    
    $query = "INSERT INTO order_header (id, orderNo, date, time, user_id, deliveryAddress, message, restaurant_id) VALUES 
    (0, '$orderNo', '$date', '$time', '$userId', '$deliveryAddress', '$message', '$restaurantId')";
    $result = mysqli_query($conn, $query);
    $response = array();


    
    if($result){
        array_push($response,
    array(
    'success' => "1",
    'message' => strval(mysqli_insert_id($conn))
    
    )
    );
    }

    
    
    
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
        mysqli_close($conn);
?>

I tested PHP with Postman, I checked the correctness of the JSON format. Postman return this, SQL works.

[
    {
        "success": "1",
        "message": "80"
    }
]

Where am I wrong?

  • This JSON response does not match the error message the least; that's array an not string - and it doesn't match the code either... without that completely useless `array_push()` it might eventually work. Forget about Postman (it's rubbish for debugging) and add an `Interceptor` instead, to see what is actually being posted and returned (that string might be an error message). – Martin Zeitler Nov 07 '20 at 18:22

0 Answers0