简体   繁体   English

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

im new with retrofit and now, when i know how to sent the normal data without any objects, just with parameters or simple body i want to know how to sent the objects... I spent like 20h to debug it and i'm confused because i dont know how to do this... There is my codes:我是 retrofit 的新手,现在,当我知道如何在没有任何对象的情况下发送普通数据时,仅使用参数或简单的主体我想知道如何发送对象......我花了大约 20 小时来调试它,我很困惑因为我不知道该怎么做...有我的代码:

API Interface: API 接口:

    @POST("/api/assortment")
    Call<PostAssortment> getAssortment(@Body String PostShipmentProgress);

PostAssortment class:邮政分类 class:

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;
    }

}

And the mainJava class:和 mainJava class:

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());
                }
            });
        }

And my retrofit onCreate:还有我的 retrofit onCreate:

        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);

And after im trying to call it im not getting any point on call enqueue just a在我尝试调用它之后,我没有得到任何调用排队的点

Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Error... Can someone describe this and help me to make it work?错误......有人可以描述这个并帮助我让它工作吗? :/ :/

You haven't provided enough information to help identify the error.您没有提供足够的信息来帮助识别错误。 Probably add the full stacktrace to the question as well.也可能将完整的堆栈跟踪添加到问题中。 But if your API post request is expecting a json body I would start with the fixes below:但是,如果您的 API 发布请求需要一个 json 主体,我将从以下修复开始:

Remove this:删除这个:

String postAssortmentJSON = gson.toJson(postAssortment);

Then pass your object as a pojo to your retrofit interface like this:然后将您的 object 作为 pojo 传递给您的 retrofit 接口,如下所示:

@POST("/api/assortment")
Call<PostAssortment> getAssortment(@Body PostAssortment postAssortment);

Then when doing your call you don't need to convert it to a string json string.然后当你打电话时,你不需要将它转换为字符串 json 字符串。 The adapter does that for you:适配器会为您做到这一点:

Call<PostAssortment> call = ApiLoginInterface.getAssortment(postAssortment);

Post assortment in my problem will be in a list, so to make it works I need to change the Call to我的问题中的帖子分类将在列表中,因此要使其正常工作,我需要将呼叫更改为

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM