Android Retrofit

0 respostas
Fgrsic

Bom dia!
Estou tendo o seguinte problema para ler dados vindos de uma URL: não consigo pegar o json de retorno. O código do response é igual a 200. Eu preciso pegar apenas o token do usuário.

Dados de entrada da url:

Dados de envio:

{

usuario: String, //obrigatório

senha: String    //obrigatório

}
Dados de Retorno:

{

hasError: Boolean,

message: String,

result: {

usuario: String,

token: String

}

}

Meu código:

public class ParametrosSerializable implements Serializable {

@SerializedName(token)

@Expose

String token;
public ParametrosSerializable() {
}

public ParametrosSerializable(String token) {
    this.token = token;
}

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

}

public class ParametrosDeserialize implements JsonDeserializer {

<a class="mention" href="/u/override">@Override</a>

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

JsonElement element = json.getAsJsonObject ();

if (json.getAsJsonObject () != null){

element = json.getAsJsonObject ();

}

return (new Gson ().fromJson (element, ParametrosSerializable.class));

}

}

public interface ServerBuscaToken {

Gson g = new GsonBuilder ().registerTypeAdapter (ParametrosSerializable.class, new ParametrosDeserialize ()).create ();

@PUT("conectariVendre/")
@Headers("Content-Type: application/json")
Call<ParametrosSerializable> putToken(@Body Map<String, String> body);

}

Este é parte do código da classe que faz a chamada:

BASE_URL = BASE_URL.concat (vEndPoint);
                    Retrofit retrofit = new Retrofit.Builder ()
                            .baseUrl (BASE_URL)
                            .addConverterFactory (GsonConverterFactory.create (g))
                            .build ();

                    final ServerBuscaToken serviceBuscaToken = retrofit.create (ServerBuscaToken.class);
                    final Call<ParametrosSerializable> requestToken = serviceBuscaToken.putToken(requestBody);
                    new Thread () {
                        @Override
                        public void run() {
                            requestToken.enqueue (new Callback<ParametrosSerializable>(){
                                @Override
                                public void onResponse(Call<ParametrosSerializable> call, Response<ParametrosSerializable> response) {
                                    if (response.code () == 200) {

                                        ParametrosSerializable retorno= response.body ();

                                        vtoken=retorno.getToken (); ===> aqui está com null
Criado 15 de outubro de 2018
Respostas 0
Participantes 1