Reputation: 325
How can I put a JSONArray
into a RequestBody
without a runtime exception being thrown?
@Multipart
@POST("/call_method")
Call<MyResponse> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);
I need to put JSONArray
into params. What I currently do:
callMethod(RequestBody.create(MediaType.parse("text/plain"), token),
RequestBody.create(MediaType.parse("text/plain"), jsonArrayParams));
But when I execute this method, I'll get a runtime exception:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line
And very intersting: The request is executed (check via CharlesProxy) but I can't see it result due to the runtime exception. How can I fix this bug?
Upvotes: 0
Views: 2951
Reputation: 6082
the execution is fine, your problem is in the response,
Expected BEGIN_OBJECT but was BEGIN_ARRAY
as you declared receiving an object Call<MyResponse>
while you are receiving an Array
you need to change the Type in the callback as following
Call<MyResponse[]> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);
OR
Call<List<MyResponse>> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);
Upvotes: 1