Retrofit 2 RequestBody JsonArray

时间:2015-12-16 14:28:42

标签: android retrofit

如果没有抛出运行时异常,如何将JSONArray放入RequestBody

@Multipart 
@POST("/call_method")
Call<MyResponse> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);

我需要将JSONArray置于参数中。我目前在做什么:

callMethod(RequestBody.create(MediaType.parse("text/plain"), token),
           RequestBody.create(MediaType.parse("text/plain"), jsonArrayParams));

但是当我执行这个方法时,我会得到一个运行时异常:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line

非常有趣:执行请求(通过CharlesProxy检查)但由于运行时异常,我无法看到它的结果。 我该如何修复这个错误?

1 个答案:

答案 0 :(得分:1)

执行没问题,你的问题出在响应中,

  

预计BEGIN_OBJECT但是BEGIN_ARRAY

当您在接收数组时声明接收对象Call<MyResponse>

您需要更改回调中的类型,如下所示

Call<MyResponse[]> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);

OR

Call<List<MyResponse>> callMethod(@Part("token")RequestBody token,@Part("params")RequestBody params);