android - Async HTTP Request and Arduino -
i developing android application communicate arduino uno though wifi. arduino programmed turn on digital port number 12 when access address 192.168.11.110. when open browner , access address red led (connected port 12) turns on, when access again turns off, works perfectly. problem in android app make http request, led turns on , seems connection "stucked", can't turn off led , can't access through browser unless restart arduino or end android activity.
the piece of code use make http request in android app this:
public class arduinorequest extends asynctask<string, string, string> { @override protected string doinbackground(string... uri) { httpclient httpclient = new defaulthttpclient(); httpresponse response; string responsestring = null; try { response = httpclient.execute(new httpget(uri[0])); statusline statusline = response.getstatusline(); if(statusline.getstatuscode() == httpstatus.sc_ok){ bytearrayoutputstream out = new bytearrayoutputstream(); response.getentity().writeto(out); responsestring = out.tostring(); out.close(); } else{ //closes connection. response.getentity().getcontent().close(); throw new ioexception(statusline.getreasonphrase()); } } catch (clientprotocolexception e) { //todo handle problems.. } catch (ioexception e) { //todo handle problems.. } return responsestring; } @override protected void onpostexecute(string result) { super.onpostexecute(result); } }
then in
menuactivity.java
i call this
arduinorequest arduinorequest = new arduinorequest(); arduinorequest.execute("http://192.168.11.110");
any idea? :)
edit:
i solved. use code instead:
// instantiate requestqueue. requestqueue queue = volley.newrequestqueue(menuactivity.this); string url ="http://192.168.11.110/"; // request string response provided url. stringrequest stringrequest = new stringrequest(request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { // display first 500 characters of response string. // mtextview.settext("response is: "+ response.substring(0,500)); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror volleyerror) { } });
Comments
Post a Comment