How to send Array of Objects in retrofit Android?

2020-07-25 01:16发布

问题:

I have an below array of objects to be passed in the service call.

    [
  {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fc250cf2497dfe5679d1"
    }
}, 
  {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385ff2f0cf2497dfe567c0c"
    }
}, 
  {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fd700cf2e65ecf6330c6"
    }
}, {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fefe0cf2497dfe567bee"
    }
}, {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385ff690cf2497dfe567c3f"
    }
}, {
    "ParkingSpace": {
        "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.55e972d21170d0c2fd7d15b1"
    }
}]

I am trying like below:

private String generateParkingspaceBody(final List<String> listOfsIds) {

        //sids array
        JSONArray sidsArray = new JSONArray();

        for (String sId: listOfsIds) {

            //creating sidObject and object
            JSONObject sIdObject = new JSONObject();
            JSONObject object = new JSONObject();


            try {
                sIdObject.put("sid", sId);
                object.put("ParkingSpace",sIdObject);
                sidsArray.put(object);
            } catch (JSONException e) {
                CPALog.e(TAG,e.getMessage());
            }



        }
        return sidsArray.toString();
    }

Sending this string into the service call like:

 Response getNearByParkingSpaces(@Header("Authorization") String accessToken,
                                    @Header("Content-Type") String contentType,
                                    @Body String arrayOfSids);

But in request showing in the logact is :

"[{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}}]"

Please help me, how to send this request?

Thanks in advance.

回答1:

You don't need to convert your object to a JSONArray, Retrofit will do it automatically for you.

Simply change your API method declaration to:

@Headers({
        "Content-type: application/json"
})
Response getNearByParkingSpaces(@Header("Authorization") String accessToken,
                                    @Body List<String> arrayOfSids);


回答2:

I encounter same issue solve this by adding this dependencies:

implementation 'com.squareup.retrofit2:converter-scalars:$version'

There are multiple existing Retrofit converters for various data formats. You can serialize and deserialize Java objects to JSON or XML or any other data format and vice versa. Within the available converters, you’ll also find a Retrofit Scalars Converter that does the job of parsing any Java primitive to be put within the request body. Conversion applies to both directions: requests and responses. https://futurestud.io/tutorials/retrofit-2-how-to-send-plain-text-request-body

then you can use your generateParkingspaceBody as value to post.

generateParkingspaceBody.toString()  as your request body