我在Serializable尝试中做错了什么

时间:2011-05-06 00:27:49

标签: java serialization

我正在尝试存储一些对象,以便在网络出现故障时进行处理。 我在Serializeable类中有一个NameValuePairs数组...我已经删除了其他所有内容,但我仍然得到一个NotSerializeableException ...我从来没有尝试过将一个Serializeable对象写入文件...我是我确定我做的很蠢,但看不出它是什么......

public void store(){
    FileOutputStream fos = null;
    ObjectOutputStream out = null;          
    try {
        Log.d(TAG, "Upload failed, saving file to " + path + "/" + fileName);
        File f = new File(path);
        if(!f.exists()){
            try{
            (new File(path)).mkdirs();
            }catch(Exception e){
                Log.d(TAG, "failed to create directory " + path );
            }
        }
        f = new File(path + "/" + fileName);
        fos = new FileOutputStream(f);
        out = new ObjectOutputStream(fos);
        Parts p = new Parts(parts);
        out.writeObject(p); //THIS IS THE LINE WHERE IT DIES WITH THAT EXCEPTION
        out.flush();
        out.close();
    } catch (IOException e) {
        Log.d(TAG, "FAILED TO SAVE file: " + path + "/" + fileName );
        Log.d(TAG, "error:" + e);
    }   catch(Exception e){     
        Log.d(TAG, "error:" + e);
    }
}

private class Parts implements Serializable{        
    private static final long serialVersionUID = -7603433209073871781L;
    @SuppressWarnings("unused")
    public NameValuePair [] p;
    public Parts(NameValuePair [] parts){
        this.p = parts;
    }       
}

2 个答案:

答案 0 :(得分:3)

如果对象实现Serializable并且其所有非瞬态和非静态字段都是可序列化的,则该对象是可序列化的。您的NameValuePair是否可序列化?

跟进:查看docsNameValuePair实施SerializableString[]也是可序列化的。你看到的例外是什么?

答案 1 :(得分:0)

您可能需要在部件中使用默认构造函数。