java - receive response from http android -
i want send url such "http://192.168.1.1/key_on" html form , receive number 1 or 0 response, if can changes correctly, in app. send request asynctask, , works correctly! don't know how response ?
here code in activities:
button btn= (button)convertview.findviewbyid(r.id.two); btn.setwidth(500); new requesttask().execute("http://192.168.1.1/key_on"); btn.setbackgroundcolor(color.parsecolor("#ff11ac06")); btn.settext(childtext); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { int color = color.transparent; drawable background = view.getbackground(); if (background instanceof colordrawable) color = ((colordrawable) background).getcolor(); if (color == color.parsecolor("#ff11ac06")) { new requesttask().execute("http://192.168.1.1/key_off"); view.setbackgroundcolor(color.parsecolor("#ff41403f")); } else { view.setbackgroundcolor(color.parsecolor("#ff11ac06")); new requesttask().execute("http://192.168.1.1/key_on"); } } }); return convertview;
and in requesttask.java:
class requesttask 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); //do response.. } }
please check request url(http://192.168.1.1/key_off) getting 404 error code.please fire url in web.
Comments
Post a Comment