java - How to send an Asynchronous POST request with a parameter in android? -
so have send asynchronous post request php sever. parameter “password” , value of “egot”. if successful return response , should display how long took. know have use asynctask how never done before in android , direction on how start or nice. please me out. code following. got far
public class pingserveractivity extends activity { private button btn6; //private edittext value; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ping_server); btn6 = (button) findviewbyid(r.id.btn_6); //value = (edittext) findviewbyid(r.id.textv5); pingbutton(); } public void pingbutton() { btn6.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // button send request server new pingposttask().execute(); } }); } private class pingposttask extends asynctask<string, integer, string>{ @override protected string doinbackground(string... params) { postdata(params[2]); return null; } protected void onpostexecute(string result) { //display result toast.maketext(getapplicationcontext(), "request sent", toast.length_long).show(); } public void postdata(string password) { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://ec2-54-243-205-92.compute-1.amazonaws.com/test/ping.php"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("egot", password)); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.ping_server, menu); return true; }
}
Comments
Post a Comment