Tôi đã tạo một dịch vụ web trong java bằng một phương thức trả về một chuỗi (một danh sách chung ở định dạng XML). Tôi tiêu thụ webservice này từ Android, và tôi nhận được chuỗi này, nhưng sau khi một số cố gắng giả lập Android chỉ bị treo khi cố gắng deserialize chuỗi. Đây là một ví dụ cho chuỗi tôi nhận được:Deserialize/unmarshal generic list từ XML thành danh sách trong Android
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<peliculas>
<pelicula>
<id>18329</id>
<poster>http://cache-cmx.netmx.mx/image/muestras/5368.rrr.jpg</poster>
<titulo>007 Operaci&oacute;n Skyfall</titulo>
</pelicula>
...
</peliculas>
Đây là lớp học trong webservice:
@XmlRootElement
public class Peliculas{
@XmlElement(name="pelicula")
protected List<Pelicula> peliculas;
public Peliculas(){ peliculas = new ArrayList<Pelicula>();}
public Peliculas(List<Pelicula> pe){
peliculas = pe;
}
public List<Pelicula> getList(){
return peliculas;
}
public void add(Pelicula pelicula) {
peliculas.add(pelicula);
}
}
__ _ __ _ __EDIT_ __ _ __ _ __ _ __ _ _
Có vẻ như bạn không thể sử dụng JAXB với Android, và có tốt hơn/thư viện nhẹ hơn cho điều đó. vì vậy tôi đã thử XML đơn giản. Đây là phương pháp:
public Peliculas unmarshal(String xml) throws Exception{
Peliculas peliculas = new Peliculas();
Serializer serializer = new Persister();
StringBuffer xmlStr = new StringBuffer(xml);
peliculas = serializer.read(Peliculas.class, (new StringReader(xmlStr.toString())) );
return peliculas;
}
NHƯNG tôi nhận được ngoại lệ này, có vẻ như nó không thể lưu dữ liệu trong đối tượng:
11-12 20:30:10.898: I/Error(1058): Element 'Pelicula' does not have a match in class app.cinemexservice.Pelicula at line 3
Dường như bạn không thể sử dụng JAXB với Android và có thư viện tốt hơn/nhẹ hơn cho điều đó ... vì vậy tôi đã thử XML đơn giản. – Pundia