Does not access the Onresponse() retrofit android method

Asked

Viewed 244 times

1

I’m making a call to a post method that returns an id generated through retrofit 2, however, in return, never access the method onResponse, I’ve tried to use other objects and formats, but none worked. Below the return of service:

 {  "_index": "smartfleet",  
    "_type": "op",   
    "_id": "AV2t5lWN2YWJ5ZApqt97",
    "_version": 1,
    "result": "created",
    "_shards": {
    "total": 3,
    "successful": 1,
    "failed": 0
},
    "created": true
}

Interface Retrofit:

@POST("op")
Call<ResponseOperacao> enviaOperacao(@Body Operacao operacao);

public static final Retrofit retrofitGraficos = new Retrofit.Builder()
        .baseUrl("https://minha.url.aqui/v1/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

The Operation class that I pass in the post method, is ok, the data is being saved in the database, the problem is only in picking up the return via onResponse().

Metodo call:RetrofitService servico = RetrofitService.retrofitGraficos.create(RetrofitService.class);
    Call<ResponseOperacao> call = servico.enviaOperacao(operacao);

    call.enqueue(new Callback<ResponseOperacao>() {
         @Override
         public void onResponse(Call<ResponseOperacao> call, Response<ResponseOperacao> response) {
             int code = response.code();
             if(code == 200){
                 System.out.println(response.body().getId());
             }
             else{
                 System.out.println(response.message());
             }
         }

         @Override
         public void onFailure(Call<ResponseOperacao> call, Throwable t) {
             System.out.println(t.getMessage());
         }
     });`

My Class Return Model:`public class Responsibility {

@SerializedName("_shards")
@Expose
private Shards shards;

@SerializedName("_index")
@Expose
private String index;
@SerializedName("_type")
@Expose
private String type;
@SerializedName("_id")
@Expose
private String id;
@SerializedName("_version")
@Expose
private Integer version;
@SerializedName("result")
@Expose
private String result;

@SerializedName("created")
@Expose
private Boolean created;`

This model class is with contrutor and the Getters and setters already implemented, I will not add the full code pro post not get too big.

  • 1

    This JSON is a little strange. It’s an object or array?!

  • An object, I’m sending the information to a nonrelational database (Elastic) and it returns me this.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.