java - Android - Cant read file, getting exception -


i have issue driving me crazy. have 2 methods, 1 reading , other writing. have class called converastion holds arraylist<chatmessage> , string properties. both converastion , chatmessage implements serializable , have know can save conversation object filesystem.

but when try read , write (update conversation), doesn't work quite well. issue on reading, java.io.eofexception when know conversation object has been written before.

here 2 methods:

public conversation getconversation(string userid, boolean createbydefault){         try {             file f = new file(sapplicationcontext.getfilesdir() + "/" + "conversation" + "/" + userid);             fileinputstream fis = new fileinputstream(f);             objectinputstream in = new objectinputstream(fis);             conversation ret = (conversation) in.readobject();             in.close();             return ret;         } catch (exception e) {             e.printstacktrace();             conversation conv = new conversation();             conv.userid = userid;             conv.usernamechat = userid + "@" + xmppcontroller.host;              if(createbydefault)                 setconversation(conv);              return conv;         }     }      public void setconversation(conversation conv){         try {             file dir = new file(sapplicationcontext.getfilesdir(), "conversation");             if(!dir.exists()) {                 dir.mkdir();             }                        file newfile = new file(sapplicationcontext.getfilesdir(), "conversation" + "/" + conv.userid);             newfile.createnewfile();             fileoutputstream fos = new fileoutputstream(newfile);             objectoutputstream out = new objectoutputstream(fos);             out.writeobject(conv);             out.close();         } catch (exception e) {             e.printstacktrace();         }      } 

now use these methods in loop, causes problem. onlistiq calles multiple times rapedly, listener called class. tried set synchronized(this) no luck, here's code:

public void onlistiq(listchatiq listiq){     // conversation                         conversation conv = getconversation(listiq.userid, true);                          // loop through messages                         for(chatiq chat : listiq.chatiqs){                             // determine if message me or other user                             string userid = listiq.userid;                             if(chat.fromme){                                 userid = currentprofile.userid;                             }                              // create new message                             chatmessage cm = new chatmessage();                             cm.id = messageid;                             cm.userid = userid;                             cm.text = body;                             cm.date = new date();                              // add message                             conv.messages.add(cm);                         }                          // update conversation messages                         setconversation(conv); } 

thanks in advance


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -