如何在文件中存储SoapObject或Envelope

时间:2014-04-17 07:32:20

标签: java android serialization ksoap2

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

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

我使用easywsdl.com网站创建了所有方法。它的自定义ExtendedSoapSerializationEnvelope具有以下定义。

public class ExtendedSoapSerializationEnvelope extends
    SoapSerializationEnvelope
{
    //Envelop code
}

我使用以下模式制作该信封Serializable。

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

有没有人有想法实现这个目标?有没有其他方法可以这样做?

2 个答案:

答案 0 :(得分:0)

嘿,这里是将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应用程序文件夹中,而不是存储在SD卡或手机存储中

答案 1 :(得分:0)

将SoapObject另存为XMl,有时直接对其进行解析可能会导致内存不足。

public boolean saveToXML(SoapObject temp) {
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();
    return false;
}