Sunday, 15 August 2010

AsyncTask HttpConnection in Android -



AsyncTask HttpConnection in Android -

i want create httpconnection using asyntask.three parameters posted server username,password , search item.the search provided user in edittext such when user clicks button,the search item sent server.i want execute doinbackground() method in onclicklistener , display response server on listviews.this asynctask class

public class posttoserver extends asynctask<string, void, string> { @override protected void onpostexecute(string result) { } @override protected string doinbackground(string... arg0) { seek { httpclient client = new defaulthttpclient(); string posturl = "url"; string username ="username"; string password = "password"; httppost post = new httppost(posturl); list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("user", username)); params.add(new basicnamevaluepair("pass", password)); urlencodedformentity ent = new urlencodedformentity(params,http.utf_8); post.setentity(ent); httpresponse responsepost = client.execute(post); httpentity resentity = responsepost.getentity(); if (resentity != null) { log.i("response",entityutils.tostring(resentity)); } } grab (exception e) { e.printstacktrace(); } homecoming null; }

this class click event called

public class startpost extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.result_page); } button submit = (button) findviewbyid(r.id.btn_search); edittext textvalue = (edittext)findviewbyid(r.id.searchedittext); string value = textvalue.gettext().tostring(); posttoserver post = new posttoserver(); checkinternetconnection check = new checkinternetconnection(null); private onclicklistener click = new onclicklistener() { @override public void onclick(final view v) { switch(v.getid()){ case r.id.btn_search: post.execute(); break; } } }; }

questions 1.what doing wrong because seems post not working , how can display server results onpostexecute()? give thanks you.

create onpostexecute method display results on textview.

protected onpostexecute (string result){ textview tv = (textview) findviewbyid(r.id.textview1); if(result != null){ tv.settext(result); } }

android-asynctask

No comments:

Post a Comment