Search code examples
javaandroidretrofitretrofit2

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:

API Interface:

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

PostAssortment 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:

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:

        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? :/


Solution

  • Post assortment in my problem will be in a list, so to make it works I need to change the Call to