简体   繁体   English

改型字符串数组发送对象表示形式而不是请求中的值。

[英]Retrofit string array sending object representation instead of value in request.

I am using Retrofit 2 with Jackson converter in my Android project with the following request: 我在Android项目中使用带有Jackson转换器的Retrofit 2和以下请求:

@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") List<String> array);

Here are the values that I pass to the interface method: 这是我传递给接口方法的值:

// the array of strings @Field param
ArrayList<String> arrayOfStrings = new ArrayList<>();
arrayOfStrings.add("1.2");
// the id param
int id = 2;

I run the app, have the API call perform the request, then retrieve the request via Charles . 我运行应用程序,让API调用执行请求,然后通过Charles检索请求。

Here is the issue. 这是问题。 The request param "array_of_strings" is being sent using the object reference, not the actual value of the array objects: 请求参数“ array_of_strings”是使用对象引用而不是数组对象的实际值发送的:

// charles request shows:
array_of_strings [I@41fea508
// instead of:
array_of_stings ["1.2"]

How do I get Retrofit/Jackson to use the value of the array objects instead of the object reference value? 如何让Retrofit / Jackson使用数组对象的值而不是对象引用值?

I guess you should just add [] brackets to array_of_strings field name, so Retrofit will know that you're going to send an array and will properly transform it to array field: 我想您应该在array_of_strings字段名称中添加[]括号,这样Retrofit就会知道您将要发送一个数组并将其正确转换为数组字段:

@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings[]") List<String> array);

Via rom4ek's comment in this answer, using ArrayList<String> as the parameter type was the solution. 通过使用rom4ek在此答案中的注释,可以使用ArrayList<String>作为参数类型。 Jackson must serialize ArrayLists differently than arrays or Lists. Jackson必须对ArrayLists进行序列化的方式与对数组或List进行序列化的方式不同。

@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") ArrayList<String> 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

暂无
暂无

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

相关问题 使用Retrofit在POST请求中发送数组列表会向对象发送内存地址,而不是发送值 - Sending array list in a POST request using Retrofit sends the object with the memory address instead of sending the values SOAP请求。 将字符串转换为字节数组 - SOAP request. Convert string to byte array 改造:使用 JSON 数组发送 POST 请求 - Retrofit: Sending POST request with JSON Array Android 广告请求。 字符串数组中的关键字 - Android ad request. Keywords from string array 在Android中进行改装-使用对象而不是json中的数组 - Retrofit in android - use object Instead of array in json 改造:发送POST请求 - Retrofit: sending POST request 发送类型为Object而不是String的对象-多态 - Sending in an object of type Object instead of String - Polymorphism 将 Retrofit 字符串值转换为日期 object - Convert Retrofit String value to a Date object 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 改装GET请求中的字符串 - String in retrofit GET request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM