2012-11-11 38 views
7

我在java中使用返回字符串(XML格式的通用列表)的方法創建了web服務。我從Android中使用這個web服務,並且我得到了這個字符串,但是在嘗試反序列化字符串之後,Android模擬器經歷了幾次嘗試之後才崩潰。這是字符串一個例子,我得到:從XML列表反序列化/解組通用列表到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> 

這是Web服務類:

@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); 
    } 
} 

__ _ __ _ __編輯_ __ _ __ _ __ _ __ _ _

好像你不能與Android使用JAXB,並有更好/輕庫這一點。所以我嘗試了簡單的XML。這是方法:

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; 
} 

,但我得到這個異常,好像它不能保存對象數據:

11-12 20:30:10.898: I/Error(1058): Element 'Pelicula' does not have a match in class app.cinemexservice.Pelicula at line 3 

回答

0

我使用SAX解析文件,然後將其手動轉換爲對象。這是代碼:

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; 
    } 

它的方式太長我的口味,但我無法弄清楚簡單的XML匹配我的類。

1

我認爲你正在做的是正確的,試試這個代碼,在給定的API。

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

您可以將Object o強制轉換爲我認爲的類型。請參考這個。 http://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Unmarshaller.html

+0

好像你不能在Android上使用JAXB,並且有更好/更輕的庫...所以我嘗試了Simple XML。 – Pundia