android - Sending ArrayList<Object> through socket. Java -
what i'm trying send , arraylist through socket android client java server. here code of client sends arraylist :
private void sendcontacts(){ apphelper helperclass = new apphelper(getapplicationcontext()); final arraylist<person> list = helperclass.getcontacts(); system.out.println("lenght of contacts array : " +list.size()); // (person person : list) { // system.out.println("name "+person.getname()+"\nnumber "+ person.getnr()); // } handler.post(new runnable() { @override public void run() { // todo auto-generated method stub try { //**151 line** os.writeobject(list); os.flush(); } catch (ioexception e) { // todo auto-generated catch block log.e(tag, "sending contact list has failed"); e.printstacktrace(); } } }); } public arraylist<person> getcontacts() { arraylist<person> alcontacts = null; contentresolver cr = mcontext.getcontentresolver(); //activity/application android.content.context cursor cursor = cr.query(contactscontract.contacts.content_uri, null, null, null, null); if(cursor.movetofirst()) { alcontacts = new arraylist<person>(); { string id = cursor.getstring(cursor.getcolumnindex(contactscontract.contacts._id)); if(integer.parseint(cursor.getstring(cursor.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) { cursor pcur = cr.query(contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id +" = ?",new string[]{ id }, null); while (pcur.movetonext()) { string contactnumber = pcur.getstring(pcur.getcolumnindex(contactscontract.commondatakinds.phone.number)); string contactname = pcur.getstring(pcur.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); person temp = new person(contactname, contactnumber); alcontacts.add(temp); break; } pcur.close(); } } while (cursor.movetonext()) ; } return alcontacts; }
here server code:
private void whilechatting() throws ioexception { abletotype(true); string message = "you connected "; sendmessage(message); {// have conversation try { message = (string) input.readobject(); // message = (string) input.readline(); showmessage("\n" + message); } catch (classnotfoundexception e) { showmessage("it not string\n"); // todo: handle exception } try{ arraylist<person> list =(arraylist<person>) input.readobject(); showmessage("got list of person size :" + list.size()); }catch(classnotfoundexception e){ showmessage("it not list of person\n"); } } while (!message.equals("client - end")); }
error code :
caused by: java.io.notserializableexception: com.lauris.client.person @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1344) @ java.io.objectoutputstream.writeobjectinternal(objectoutputstream.java:1651) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1497) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1461) @ java.util.arraylist.writeobject(arraylist.java:648) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ java.io.objectoutputstream.writehierarchy(objectoutputstream.java:1033) @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1384) @ java.io.objectoutputstream.writeobjectinternal(objectoutputstream.java:1651) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1497) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1461) **at com.lauris.client.mainactivity$clientthread$4.run(mainactivity.java:151)** @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
i guess problem serialization ? don't understand meaning of ? can explain me is, why need it? , how may fix code?
additional question. can see in code im trying listen different inputstreams. think i,m doing wrong. more advanced explain me how correct ?
appreciate help, i'm stuck on this.
the person
class must implement serializable
:
import java.io.serializable; public class person implements serializable { /** * */ private static final long serialversionuid = 1l; }
Comments
Post a Comment