我有一个Web服务,它返回一个对象Home的ArrayList ...这是我的类Home:
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
public class Home implements Serializable {
public void setPercorso(String percorso) {
this.percorso = percorso;
}
public String getPercorso() {
return percorso;
}
public void setNomignolo(String nomignolo) {
this.nomignolo = nomignolo;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setUtente(String utente) {
this.utente = utente;
}
public void setFamiglia(String famiglia) {
this.famiglia = famiglia;
}
public void setFoto(byte[] foto) {
this.foto = foto;
}
public String getNomignolo() {
return nomignolo;
}
public String getNome() {
return nome;
}
public String getUtente() {
return utente;
}
public String getFamiglia() {
return famiglia;
}
public byte[] getFoto() {
return foto;
}
public Home(String nomignolo, String nome, String utente, String famiglia, byte[] foto, String percorso) {
this.nomignolo = nomignolo;
this.nome = nome;
this.utente = utente;
this.famiglia = famiglia;
this.foto = foto;
this.percorso = percorso;
}
public Home() {
}
private String nomignolo;
private String nome;
private String utente;
private String famiglia;
private byte[] foto;
private String percorso;
public ArrayList<Home> caricaH() throws SQLException, ClassNotFoundException, MyException, FileNotFoundException {
ArrayList<Home> list = new ArrayList<Home>();
list= HomeDAO.caricaHome(this);
return list;
}
}
在Android部分,我以这种方式接收数据:
// other code bla bla bla...
public static ArrayList<Home> invokeHomeWS(String userName, String webMethName) {
boolean homeStatus = false;
ArrayList<Home> list = new ArrayList<Home>();
SoapObject request = new SoapObject(NAMESPACE, webMethName);
PropertyInfo unamePI = new PropertyInfo();
// Set Username
unamePI.setName("username");
// Set Value
unamePI.setValue(userName);
// Set dataType
unamePI.setType(String.class);
// Add the property to request object
request.addProperty(unamePI);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);
// Get the response
java.util.Vector<SoapObject> rs = (java.util.Vector<SoapObject>) envelope.getResponse();
for(SoapObject cs : rs){
Home item = new Home();
item.setNomignolo(cs.getPropertyAsString("nomignolo"));
item.setUtente(cs.getPropertyAsString("utente"));
item.setNome(cs.getPropertyAsString("nome"));
item.setPercorso(cs.getPropertyAsString("percorso"));
item.setFoto((byte[])cs.getProperty("foto"));
item.setFamiglia(cs.getPropertyAsString("famiglia"));
list.add(item);
}
} catch (Exception e) {
//Assign Error Status true in static variable 'errored'
HomeActivity.errored = true;
e.printStackTrace();
}
return list;
}
一切正常,除了
item.setFoto((byte[])cs.getProperty("foto"));...
它会产生错误:
W / System.err:java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive不能转换为byte []。
所以我的问题是:我如何从SoapObject获取字节数组?
答案 0 :(得分:0)
如果返回String,可以将其转换为byte [],如下所示。试试这个。
byte[] b = string.getBytes();
答案 1 :(得分:0)
试试这个
byte[] bitmapdata = blob.toByteArray();
如果位图数据是字节数组,则获取位图就像这样
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
希望得到这个帮助。