What am I doing wrong in this Serializable attempt
I'm trying to store some objects for future processing in event of network failure. I have an array of NameValuePairs in a Serializeable class... I've eliminated everything else, and still I'm getting a NotSerializeableException... I've never tried to write a Serializeable object to file before... I'm sure I'm doing something stupid, but can't see what it is...
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 = 开发者_StackOverflowparts;
}
}
An object is serializable if it implements Serializable
and all of its non-transient and non-static fields are serializable. Is your NameValuePair
serializable?
Follow-up: Looking at the docs, NameValuePair
implements Serializable
. Also String[]
is serializable. What is the exception you're seeing?
You may need default constructor in Parts.
精彩评论