Tecnologia do Blogger.
RSS

[androidbrasil-dev] Re: Pegar retorno em json para preecher objeto

Olá, Duk, não tenho muita experiência com JSON mas vou tentar ajudar: tente remover as aspas do seu comando de POST.
 Acredito que seja por isso que ele está interpretando seu JSON como uma STRING.

Em domingo, 25 de agosto de 2013 18h39min53s UTC-3, Duk escreveu:

Tenho o seguinte retorno do meu webservice criado em ruby on rails

{"created_at":"2013-08-24T02:45:13Z","datebirth":null,"email":"em...@gmail.com","firstname":"Ricardo","gender":null,"id":11,"lastname":null,"modelphone":null,"password":null,"password_confirmation":null,"picture":null,"token":"98e58617a9beb77c71de20b3545550e4","typeos":null,"updated_at":"2013-08-25T20:45:25Z"}

Esse retorno eu tenho digitando o curl no console assim:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/sessions.json -d "{\"user\":{\"email\":\"em...@gmail.com\",\"password\":\"secret1234\"}}"

e também tenho esse mesmo retorno no Android!

Estou usando AndroidAnnotations e criei o código que chama o webservice

@Background

void login(UserWrapper userWrapper){

try {

//Acessa o webservice através de rest passando o objeto UserWrapper que tem o Objeto User

JsonObject json = userRestClient.login(userWrapper);

Gson gson = new Gson();

userWrapper = gson.fromJson(json, UserWrapper.class);

sucessLogin(userWrapper);

} catch (RestClientException e) {

errorLogin();

}

}

Mas estou recebendo o seguinte erro

08-24 18:34:19.420: E/LoginActivity_(11373): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

Minha classe RestClient está da seguinte forma

@Rest(rootUrl="http://192.168.1.100:3000", converters={GsonHttpMessageConverter.class}) @Accept(MediaType.APPLICATION_JSON) public interface UserRestClient {

@Post("/api/v1/registrations")

UserWrapper addUser(UserWrapper user);

@Post("/api/v1/sessions.json")

JsonObject login(UserWrapper user);

@Get("/users/{id}.json")

JsonObject edit(int id);

@Put("/users/{id}")

void update(User user, int id);

@Post("/user")

void recoverPassWordUser(String email);

}

Sei que vem uma String do meu servidor, porém preciso receber um JsonObject. Preciso usar um parse, então fiz da seguinte maneira alterando meu código

String json = userRestClient.login(userWrapper);

JsonParser parser = new JsonParser();

JsonObject jsonObject = parser.parse(json).getAsJsonObject();

Gson gson = new Gson();

userWrapper = gson.fromJson(jsonObject, UserWrapper.class);

sucessLogin(userWrapper);

Porém recebo o erro o seguinte erro:

08-25 18:31:17.945: E/LoginActivity_(13266): org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2

Está meio que tipo, quando mudo pra receber um JsonObject ele fala que é uma String, quando mudo pra receber uma String ele fala que é um JsonObject.

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário