gpt4 book ai didi

java - 如何从 SoapObject 获取 byte[]

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:17 25 4
gpt4 key购买 nike

我有一个 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 cannot be cast to byte[].

所以我的问题是:我如何从 SoapObject 获取字节数组?

最佳答案

如果是返回String,可以将其转换为byte[],如下所示。试试这个。

byte[] b = string.getBytes();

关于java - 如何从 SoapObject 获取 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33034477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com