2012-11-11 24 views
7

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&amp;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 

Trả lời

0

Tôi đã sử dụng SAX để phân tích cú pháp tệp và sau đó chuyển đổi thủ công thành đối tượng. Đây là mã:

public List<Pelicula> unmarshal(String xml) throws Exception{   
     List<Pelicula> peliculas = new ArrayList<Pelicula>();  
     InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8")); 
     XmlPullParser parser = Xml.newPullParser(); 
     char[] c; 
     String id="", titulo="", poster="", atributo=""; 
     int datos =0; 
     try{ 
      parser.setInput(is, "UTF-8"); 
      int event = parser.next(); 
     while(event != XmlPullParser.END_DOCUMENT) { 
      if(event == XmlPullParser.START_TAG) { 
       Log.d(TAG, "<"+ parser.getName() + ">"); 
       atributo = parser.getName(); 
       for(int i = 0; i < parser.getAttributeCount(); i++) { 
        Log.d(TAG, "\t"+ parser.getAttributeName(i) + " = "+ parser.getAttributeValue(i)); 
       } 
      } 
      if(event == XmlPullParser.TEXT&& parser.getText().trim().length() != 0) 
      { 
       Log.d(TAG, "\t\t"+ parser.getText()); 
       if (atributo=="id"){id=parser.getText(); datos++;} 
       else if(atributo=="titulo"){titulo=parser.getText(); datos++;} 
       else if(atributo=="poster"){poster=parser.getText(); datos++;} 
       if(datos==3){peliculas.add(new Pelicula(id, titulo, poster)); datos=0;} 
      } 
       if(event == XmlPullParser.END_TAG) 
        Log.d(TAG, "</"+ parser.getName() + ">");    
       event = parser.next(); 

      is.close(); 
     } 
     } catch(Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); }   
     for (Pelicula p : peliculas){ 
      Log.d("Película en lista: ", p.titulo); 
     }   
     return peliculas; 
    } 

Quá dài đối với khẩu vị của tôi, nhưng tôi không thể tìm ra XML đơn giản để phù hợp với lớp học của mình.

1

Tôi nghĩ rằng bạn đang làm đúng, hãy thử mã này được đưa ra trong API.

JAXBContext jc = JAXBContext.newInstance("add your class's full qualified class name here"); 
Unmarshaller u = jc.createUnmarshaller(); 
Object o = u.unmarshal(xmlSource); 

Bạn có thể truyền Object o theo loại của mình. Vui lòng tham khảo. http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html

+0

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

Các vấn đề liên quan