Android JSON must start with an array or an object._json data must be an object. found array.

我用Retrofit POST方法上传头像,每次一点上传就传回JSON must start with an array or an object.困扰了很久,一直没有头绪,今天,终于解决了。

//上传头像
@Multipart
@POST("xxx/photo")
Call<ResponseBody> upPhoto(@Part("photo") RequestBody body, @Part("username") RequestBody username);

注意:类型必须是 RequestBody

大家可以看到,我写的是两个参数的,一般上传图片一个参数就行了,但是上传头像,必须知道是谁的头像,对吧,所以上传了一个用户名和头像,用户名我一开始写的String类型,所以老是报JSON must start with an array or an object错误,当我都改成RequestBody后,问题就解决了。

参考链接retrofit使用注意事项_ITurbo的博客-CSDN博客