Monday, 15 March 2010

java - Keeping a socket connection open while sending commands on Android -



java - Keeping a socket connection open while sending commands on Android -

this week started android programming, , i'd accomplish through first application. i'm new both java , android, have python experience.

i have python server running on raspberrypi that, depending on commands received, fades bunch of rgb leds.

i'm trying build android app sends commands server through sockets.

there input box , button: when button pressed, want entered info sent server.

i wrote , works, believe there wrong implementation. want app open socket connection and, while keeping open, send commands server.

when clicked, button passes entered command , server address asynctask, opens connection server , sends command. however, send new command, connection has closed , opened again, , asynctask called time.

i don't need this: connection should remain open time.

here's code (the of import bits):

this gets input , sends asynctask when button clicked:

protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext inputtext = (edittext) findviewbyid(r.id.edittext2); final button sendbutton = (button) findviewbyid(r.id.button); final textview text = (textview) findviewbyid(r.id.textview3); sendbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string outputtext = inputtext.gettext().tostring().concat("\n"); text.append(outputtext); connecttask task = new connecttask(); task.execute("192.168.1.3:1322", inputtext.gettext().tostring()); } });

and asynctask subclass:

private class connecttask extends asynctask<string, void, void>{ @override protected void doinbackground(string... strings){ string[] spl = strings[0].split(":"); string address = spl[0]; int port = integer.parseint(spl[1]); socketaddress sockaddr = new inetsocketaddress(address, port); socket socket = new socket(); try{ socket.connect(sockaddr, 5000); } grab (ioexception e){ e.printstacktrace(); } outputstream os = null; seek { os = socket.getoutputstream(); } grab (ioexception e) { e.printstacktrace(); } outputstreamwriter osw = new outputstreamwriter(os); bufferedwriter bw = new bufferedwriter(osw); seek { bw.write(strings[1]); } grab (ioexception e) { e.printstacktrace(); } seek { bw.flush(); } grab (ioexception e) { e.printstacktrace(); } seek { socket.close(); } grab (ioexception e) { e.printstacktrace(); } homecoming null; } }

thanks in advance help!

what want called service. services bit complex, , more involved reply want type here, here's few things:

you want bound service, can send messages app service , back. you might @ coursera course of study https://class.coursera.org/posa-002/ , covering similar want. read documents linked above. services need careful thought, documents good.

services because operate in background, can on own thread, , interact activities , other services. open connection on connect, talk in background sending messages , forth between activity , service, , disconnect service when done. relatively straightforward, although involve complex connections.

java android sockets android-asynctask

No comments:

Post a Comment