gpt4 book ai didi

java - 如何在文件中存储 SoapObject 或 Envelope

转载 作者:行者123 更新时间:2023-11-29 08:50:10 29 4
gpt4 key购买 nike

我正在尝试将 SoapObjectEnvelope 存储在一个文件中,但认为它不是可序列化的,因此它没有存储在文件中。

我的目标是将 SoapObjectEnvelope 一次存储在文件中,下次再存储一次,而不是构建一个新的 SoapObjectEnvelope ,从存储的文件中获取该对象。

我已经使用 easywsdl.com 创建了所有方法地点。它的自定义 ExtendedSoapSerializationEnvelope 具有以下定义。

public class ExtendedSoapSerializationEnvelope extends
SoapSerializationEnvelope
{
//Envelop code
}

我使用以下模式使信封可序列化。

public class ExtendedSoapSerializationEnvelope extends
SoapSerializationEnvelope implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
}

并在文件中使用以下代码编写。

public static void writeEnvelope(Object object) throws IOException {
File file = new File(Environment.getExternalStorageDirectory()
.getAbsoluteFile() + File.separator + "envelope.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}

它成功写入 Envelope 但是当我尝试使用以下代码从文件中读取该对象时,

public static Object readEnvelope() throws IOException,
ClassNotFoundException {
File file = new File(Environment.getExternalStorageDirectory()
.getAbsoluteFile() + File.separator + "envelope.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}

它在异常之后触发,

java.io.InvalidClassException: org.ksoap2.serialization.SoapSerializationEnvelope; IllegalAccessException

有没有人有实现这一目标的好主意?还有其他方法吗?

最佳答案

嘿,这是将 SoapObject 保存到文件并从文件中获取它的方法。

public boolean saveToFile(SoapObject temp) {
Log.d("DEBUG", "saveFile");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(temp);
XmlSerializer aSerializer = Xml.newSerializer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
aSerializer.setOutput(os, "UTF-8");
envelope.write(aSerializer);
aSerializer.flush();
} catch (Exception e) {
e.printStackTrace();
}
byte[] bytes = os.toByteArray();
try {
FileOutputStream fos = _context.openFileOutput("response" + ".xml", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(bytes);
oos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
Log.d("DEBUG", "IOException " + e.getMessage());
return false;
}
}


public SoapObject getFromFile() {
Log.d("DEBUG", "getFile");
ObjectInputStream input;
String filename = "response.xml";
try {
FileInputStream fis = _context.openFileInput(filename);
input = new ObjectInputStream(fis);
byte[] bytes = (byte[]) input.readObject();
input.close();
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject soap = null;
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
XmlPullParser p = Xml.newPullParser();
p.setInput(inputStream, "UTF-8");
envelope.parse(p);
soap = (SoapObject) envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
Log.d("DEBUG", "Exception " + e.getLocalizedMessage());
}
return soap;
} catch (StreamCorruptedException e) {
e.printStackTrace();
Log.d("DEBUG", "StreamCorruptedException " + e.getLocalizedMessage());
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("DEBUG", "FileNotFoundException " + e.getLocalizedMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.d("DEBUG", "IOException " + e.getLocalizedMessage());
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.d("DEBUG", "ClassNotFoundException " + e.getLocalizedMessage());
return null;
}
}

它存储在 android 应用程序文件夹中,而不是在 sdcard 或手机存储中

关于java - 如何在文件中存储 SoapObject 或 Envelope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127163/

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