简体   繁体   English

使用Retrofit在POST请求中发送数组列表会向对象发送内存地址,而不是发送值

[英]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; 
    }
}

Android Retrofit 2 - 发送数组时出现问题<object>使用 POST<div id="text_translate"><p> 我是 retrofit 的新手,现在,当我知道如何在没有任何对象的情况下发送普通数据时,仅使用参数或简单的主体我想知道如何发送对象......我花了大约 20 小时来调试它,我很困惑因为我不知道该怎么做...有我的代码:</p><p> API 接口:</p><pre> @POST("/api/assortment") Call<PostAssortment> getAssortment(@Body String PostShipmentProgress);</pre><p> 邮政分类 class:</p><pre> public class PostAssortment { private String JSON; @SerializedName("token") @Expose private String token; @SerializedName("assortment") @Expose private Assortment assortment; @SerializedName("tokens") @Expose private List<String> tokens; @SerializedName("positions") @Expose private List<Position> positions; @SerializedName("deviceId") @Expose private String deviceId; public String getToken() { return token; } public void setToken(String token) { this.token = token; } public Shipment getAssortment() { return assortment; } public void setAssortment(Assortment assortment) { this.assortment = assortment; } public List<String> getTokens() { return tokens; } public void setTokens(List<String> tokens) { this.tokens = tokens; } public List<Position> getPositions() { return positions; } public void setPositions(List<Position> positions) { this.positions = positions; } public String getDeviceId() { return deviceId; } public void setDeviceId(String deviceId) { this.deviceId = deviceId; } public String getJSON() { return JSON; } public void setJSON(String JSON) { this.JSON = JSON; } }</pre><p> 和 mainJava class:</p><pre> Gson gson = new Gson(); PostAssortment postAssortment= new PostAssortment(); List<String> tokens = new ArrayList<>(); tokens.add("someToken"); postAssortment.setTokens(tokens); postAssortment.setDeviceId("aaaaa"); List<Position> currentPosition = new ArrayList<>(); Position cp = new Position(); cp.setItemName("Some"); cp.setPlace("POLAND"); cp.setTimestamp("2020-12-09T11:00:00"); currentPosition.add(cp); postAssortment.setPositions(currentPosition); String postAssortmentJSON = gson.toJson(postAssortment); Call<PostAssortment> call = ApiLoginInterface.getAssortment(postAssortmentJSON); call.enqueue(new Callback<PostAssortment>() { @Override public void onResponse(Call<PostAssortment> call, Response<PostAssortment> response) { PostAssortment assortmentResponse = response.body(); } @Override public void onFailure(Call<PostAssortment> call, Throwable t) { Log.d("FAILURE", "onFailure: " + t.getMessage()); } }); }</pre><p> 还有我的 retrofit onCreate:</p><pre> Gson gson = new GsonBuilder().setLenient().create(); String BASE_URL = getString(API_URL); Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build(); ApiLoginInterface = retrofit.create(ApiLoginInterface.class);</pre><p> 在我尝试调用它之后,我没有得到任何调用排队的点</p><pre>Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference</pre><p> 错误......有人可以描述这个并帮助我让它工作吗? :/</p></div></object> - Android Retrofit 2 - problem with sending the Array<Object> using POST

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 改型字符串数组发送对象表示形式而不是请求中的值。 - Retrofit string array sending object representation instead of value in request. 改造:发送POST请求 - Retrofit: sending POST request 改造:使用 JSON 数组发送 POST 请求 - Retrofit: Sending POST request with JSON Array Android Retrofit 2 - 发送数组时出现问题<object>使用 POST<div id="text_translate"><p> 我是 retrofit 的新手,现在,当我知道如何在没有任何对象的情况下发送普通数据时,仅使用参数或简单的主体我想知道如何发送对象......我花了大约 20 小时来调试它,我很困惑因为我不知道该怎么做...有我的代码:</p><p> API 接口:</p><pre> @POST("/api/assortment") Call<PostAssortment> getAssortment(@Body String PostShipmentProgress);</pre><p> 邮政分类 class:</p><pre> public class PostAssortment { private String JSON; @SerializedName("token") @Expose private String token; @SerializedName("assortment") @Expose private Assortment assortment; @SerializedName("tokens") @Expose private List<String> tokens; @SerializedName("positions") @Expose private List<Position> positions; @SerializedName("deviceId") @Expose private String deviceId; public String getToken() { return token; } public void setToken(String token) { this.token = token; } public Shipment getAssortment() { return assortment; } public void setAssortment(Assortment assortment) { this.assortment = assortment; } public List<String> getTokens() { return tokens; } public void setTokens(List<String> tokens) { this.tokens = tokens; } public List<Position> getPositions() { return positions; } public void setPositions(List<Position> positions) { this.positions = positions; } public String getDeviceId() { return deviceId; } public void setDeviceId(String deviceId) { this.deviceId = deviceId; } public String getJSON() { return JSON; } public void setJSON(String JSON) { this.JSON = JSON; } }</pre><p> 和 mainJava class:</p><pre> Gson gson = new Gson(); PostAssortment postAssortment= new PostAssortment(); List<String> tokens = new ArrayList<>(); tokens.add("someToken"); postAssortment.setTokens(tokens); postAssortment.setDeviceId("aaaaa"); List<Position> currentPosition = new ArrayList<>(); Position cp = new Position(); cp.setItemName("Some"); cp.setPlace("POLAND"); cp.setTimestamp("2020-12-09T11:00:00"); currentPosition.add(cp); postAssortment.setPositions(currentPosition); String postAssortmentJSON = gson.toJson(postAssortment); Call<PostAssortment> call = ApiLoginInterface.getAssortment(postAssortmentJSON); call.enqueue(new Callback<PostAssortment>() { @Override public void onResponse(Call<PostAssortment> call, Response<PostAssortment> response) { PostAssortment assortmentResponse = response.body(); } @Override public void onFailure(Call<PostAssortment> call, Throwable t) { Log.d("FAILURE", "onFailure: " + t.getMessage()); } }); }</pre><p> 还有我的 retrofit onCreate:</p><pre> Gson gson = new GsonBuilder().setLenient().create(); String BASE_URL = getString(API_URL); Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build(); ApiLoginInterface = retrofit.create(ApiLoginInterface.class);</pre><p> 在我尝试调用它之后,我没有得到任何调用排队的点</p><pre>Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference</pre><p> 错误......有人可以描述这个并帮助我让它工作吗? :/</p></div></object> - Android Retrofit 2 - problem with sending the Array<Object> using POST 改造发布请求发送空对象 - Retrofit post request sends null object 使用 Retrofit2 在 form-urlencoded 请求中发送对象列表 - Sending list of objects in form-urlencoded request using Retrofit2 改造POST将错误的值发送到服务器 - Retrofit POST sending the wrong values to server 使用 react.js object 发送 null 值的 POST 请求 - POST request with react.js object sending null values 使用Retrofit发送POST请求时无法获取POST参数 - Can not get POST parameters when sending POST request with Retrofit 转换列表<Object>字符串数组并使用java邮件将电子邮件发送到多个地址 - converting List<Object> to string array and sending email to multiple address using java mail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM