I need to consume a web service (WS) REST in my android application. The web service was made with glassfish, jersey and ORM hibernate to communicate with the database. Here is the WS code:
Vegetarian Service:
@Path("/vegetariano")
public class VegetarianoService {
@GET
@Path("buscar/{id}")
@Produces(MediaType.APPLICATION_XML)
public Vegetariano consultar(@PathParam("id") int id) {
return new Vegetariano("Lucas Kauer", new Date(), Genero.MASCULINO, "1234", "123", TipoDeVegetariano.LACTOVEGETARIANO,
new ArrayList<Alergia>(), "[email protected]", "aa123");
}
}
Vegetarian
@XmlRootElement
@Entity
@Table(name = "Vegetariano")
public class Vegetariano{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Basic(optional = false)
@Column(length = 100)
private String nomeCompleto;
@Basic(optional = false)
@Temporal(TemporalType.DATE)
private Date dataNascimento;
@Basic(optional = false)
@Enumerated(EnumType.STRING)
private Genero genero;
@Basic(optional = false)
@Column(length = 9)
private String cep;
@Basic(optional = true)
@Column(length = 50)
private String complemento;
@Enumerated(EnumType.STRING)
@Basic(optional = false)
private TipoDeVegetariano tipoVegetariano;
@Basic(optional = false)
private String email;
@Basic(optional = false)
@Column(length = 10)
private String senha;
@Basic(optional = true)
@OneToMany(mappedBy = "usuarioVegetariano", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Alergia.class)
private List<Alergia> alergias = new ArrayList<Alergia>();
@Basic(optional = true)
@OneToMany(fetch = FetchType.EAGER, targetEntity = Diario.class)
private List<Diario> diarios = new ArrayList<Diario>();
public Vegetariano() { }
public Vegetariano(String nome, Date dataNascimento, Genero genero, String cep, String complemento,
TipoDeVegetariano tipoVegetariano, List<Alergia> alergias, String email, String senha){
this.nomeCompleto = nome;
this.dataNascimento = dataNascimento;
this.genero = genero;
this.cep = cep;
this.complemento = complemento;
this.tipoVegetariano = tipoVegetariano;
this.alergias = alergias;
this.email = email;
this.senha = senha;
}
}
When trying to consume in my android application the following error occurs:
Could not read [class pojos.Vegetarian]
And the following cause:
Unparseable date: "2016-11-08T21: 43: 00.116-02: 00" (at offset 10)
Follow the Android code below:
Vegetarian consumer: public class VegetarianConsumer {
private RestTemplate restTemplate;
private static final String URL_BASE = "http://10.0.2.2:8080/Rest2016/vegeryday/vegetariano";
public VegetarianoConsumer(){
this.restTemplate = new RestTemplate();
this.restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
}
public Vegetariano chamaMetodoConsultar(int id){
String URL = URL_BASE + "/buscar/{id}";
Map<String, Integer> meuMap = new HashMap<>();
meuMap.put("id", id);
Vegetariano vegetariano = restTemplate.getForObject(URL, Vegetariano.class, meuMap);
return vegetariano;
}
}
Vegetarian:
public class Vegetariano{
private int id;
private String nomeCompleto;
private Date dataNascimento;
private Genero genero;
private String cep;
private String complemento;
private TipoDeVegetariano tipoVegetariano;
private String email;
private String senha;
private List<Alergia> alergias = new ArrayList<Alergia>();
private List<Diario> diarios = new ArrayList<Diario>();
private Vegetariano() {}
public Vegetariano(String nome, Date dataNascimento, Genero genero, String cep, String complemento,
TipoDeVegetariano tipoVegetariano, List<Alergia> alergias, String email, String senha){
this.nomeCompleto = nome;
this.dataNascimento = dataNascimento;
this.genero = genero;
this.cep = cep;
this.complemento = complemento;
this.tipoVegetariano = tipoVegetariano;
this.alergias = alergias;
this.email = email;
this.senha = senha;
}
}
The web service is returning the following XML:
<vegetariano>
<cep>1234</cep>
<complemento>123</complemento>
<dataNascimento>2016-11-08T21:44:08.398-02:00</dataNascimento>
<email>[email protected]</email>
<genero>MASCULINO</genero>
<id>0</id>
<nomeCompleto>Lucas Kauer</nomeCompleto>
<senha>aa123</senha>
<tipoVegetariano>LACTOVEGETARIANO</tipoVegetariano>
</vegetariano>
I believe the problem is the format in which <dataNascimento>
is being filled. Does anyone know anything that might help me?