[英]Sending array list in a POST request using Retrofit sends the object with the memory address instead of sending the values
I have a class: 我有一堂课:
public class PetModel{
Serialized("cat")
String cat;
Serialized("dog")
String dog;
// getters and setters
}
when I do a POST using retrofit like so: 当我使用这样的改造进行POST时:
@FormUrlEncoded
@POST("/pet/{id}")
Pets postPets(@Path("id") String id,@Field("pets") ArrayList<PetModel> pets);
In the logs I see the request sending <package_name>.PetModel@cc49e70
instead of sending the values itself. 在日志中,我看到请求发送<package_name>.PetModel@cc49e70
而不是发送值本身。 What am I doing wrong? 我究竟做错了什么?
Please try bellow method, may be you will got solution 请尝试以下方法,也许您会找到解决方法
PrintWriter out = response.getWriter();
List<Countries> country = new ArrayList<Countries>();
country = FetchData.getAllCountries();
JSONObject js = new JSONObject();
js.put("countries", country); // make sure the Country class overrides toString()
// set the response content-type
response.setContentType("application/json");
// writing the json-array to the output stream
out.print(js.toJSONString());
out.flush();
If you have access to the server side code and can change it just add a viewmodel or something as a class containing the array list of generic type T. Then every where of working with arraylists use that. 如果您有权访问服务器端代码并可以对其进行更改,则只需添加一个视图模型或某些类作为包含通用类型T的数组列表的类即可。然后,使用数组列表的所有地方都将使用它。
There is similar problem when retrieving String type from server which can be solved same way. 从服务器检索字符串类型时,存在类似的问题,可以用相同的方法解决。
Override toString()
method from your PetModel
class. 从您的PetModel
类重写toString()
方法。
public class PetModel {
Serialized("cat")
String cat;
Serialized("dog")
String dog;
// getters and setters
@Override
public String toString() {
return "Cat " + cat + " Dog " + dog;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.