android - client can't send message from separate class -
i created socket program on android studio. first create on main class , working. of site separate connect , disconnect works. there problem when client send message on server. whenever put letter, output blank space.
separate class
public class socketclient extends thread { string serverm = null; socket client; string socketdata; context context; string message; string address; public boolean mrun = true; private printwriter printwriter; public socketclient (string address, context context, string message ) { this.address = address; this.context = context; this.message = message; } @override public void run(){ mrun = true; try { client = new socket(address, 3818); bufferedreader mbufferin = new bufferedreader( new inputstreamreader(client.getinputstream())); while (mrun) { serverm = mbufferin.readline(); if (serverm != null) { system.out.println(serverm); socketdata = serverm; } } } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } public void stopsocket() { if(client !=null) { toast.maketext(context, "disconnected", toast.length_long).show(); try { client.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } else{ toast.maketext(context, "socket not found", toast.length_long).show(); } } public void sendsocket() { if (message != null) { try { printwriter = new printwriter(client.getoutputstream(),true); printwriter.write(message + "\r\n"); // write message output stream printwriter.flush(); }catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } toast.maketext(context, "sent", toast.length_long).show(); }
}
main class
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textfield = (edittext) findviewbyid(r.id.message); // reference text field connect = (button) findviewbyid(r.id.connect); // reference connect button disconnect = (button) findviewbyid(r.id.disconnect); // reference connect button send = (button) findviewbyid(r.id.send); // reference send button edittextaddress = (edittext) findviewbyid(r.id.address); // reference address disconnect.setonclicklistener(disconnectonclicklistener); connect.setonclicklistener(connectonclicklistener); send.setonclicklistener(sendonclicklistener); } //button send onclicklistener sendonclicklistener = new onclicklistener() { public void onclick(view v) { thread.sendsocket(); textfield.settext(""); // reset text field blank } }; //button disconnect onclicklistener disconnectonclicklistener = new onclicklistener() { public void onclick(view v) { thread.stopsocket(); } }; //button connect onclicklistener connectonclicklistener = new onclicklistener() { public void onclick(view v) { toast.maketext(getapplicationcontext(),"connected",toast.length_long).show(); thread = new socketclient(edittextaddress.gettext().tostring(), getapplicationcontext(), textfield.gettext().tostring()); thread.start(); } };
now know i'm missing. sorry inconvenience.
separate class
public void sendsocket(string newmessage ) { if (newmessage != null) { try { printwriter = new printwriter(socket.getoutputstream(),true); printwriter.write(newmessage + "\r\n"); // write message output stream printwriter.flush(); }catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
}
main class
view.onclicklistener sendonclicklistener = new view.onclicklistener() { public void onclick(view v) { thread.sendsocket(textfield.gettext().tostring()); } };
Comments
Post a Comment