Retrofit 2 Android objects

Asked

Viewed 421 times

1

There’s no way I can get the valuables.

The value received by JSON are these:

{
    "status": true,
    "valores": {
        "galo": {
            "nome": "galinho",
            "valor": 300,
            "ultima_consulta": 1386349203,
            "fonte": "galo.com.br"
        }
    }
}

I’ve tried everything, nothing’s right.

RespostaServidor respostaServidor = response.body();

Valores valores = respostaServidor.getValores();
galo = valores.getGalo();

In class respostaServidor, he gets the status right, and the object, but then passes the null value to the rooster.

I don’t know what else to do.

  • Rooster is an object within json, a "son". For example: { "request": 1, "items": { "codItem": 1 } } jsonObject objectPedido = json.getObject("request"); .... , and then read the items like this: objectPedido.getObject("items");

  • Post the class Values.

2 answers

1

public static final String BASE_URL = "http://portaljuventude.includetecnologia.com.br/api/";

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder
        =
        new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(new Gson()));

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

1

Hello. Let’s assume you’ve done something like this:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://exemplo.com")
            .build();

I also use retrofit. And to parse the json for objects, I am using Gsonconverterfactory. Just adding in the dependencies of Gradle:

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

So building the retrofit this way:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://exemplo.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

If you’re saying that in your code, maybe that’s the problem.

It would be interesting to detail a little more about how you are working with the retrofit.

Browser other questions tagged

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