android - Using AsyncTask to implement API and updating result to UI -
i'm using asynctask in api carry out network operations. @ ui part there button, clicking on triggers api execute request/async task. api implementing callback updated onpostexecute() method. api gets right data populated once onpostexecute() passes data on callback. works good. i'm getting conceptual problem while updating ui. want achieve: 1- click "button" on ui 2- want update api's response string ui. response string after executing asynctask.
the problem api return null response in current thread of execution. once ui thread done, see api/asynctask data coming in. know i'm missing trivial, important. know whats right way update api response on ui?
[update]
here ui code includes clicking button triggering async task via api (just class implements callbacks getting server response)
button.setonclicklistener(new onclicklistener() { public void onclick(view v) { api api = new api(); request request = new request(); request.seturl("http://www.example.com/"); api.fetch(request); try { //it doesn't matter how wait here return response. //response task doesn't return in thread. //if click button "refresh" refresh data, api.getresponsestring() shows response server. thread.sleep(2000); //api.getresponsestring() returns null. expect return data retrieved server string text = api.getresponsestring(); system.out.println("text: " + text); textview.settext(text); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } });
api code
public class api implements responsecallbacks { string responsestring = null; public void fetch(request request) { makerequest(request, this); } public void makerequest(request request, final responsecallbacks responsecallbacks) { mytask asynctask = new mytask(); asynctask.setresponsecallbacks(responsecallbacks); asynctask.execute(request); } @override public void onsuccess(response response) throws ioexception { if (response.issuccess()) { responsestring = "success"; } else { responsestring = "failed"; } } public string getresponsestring() { return responsestring; } }
asynctask code
public class mytask extends asynctask { private responsecallbacks responsecallbacks; public mytask() {} public void setresponsecallbacks(responsecallbacks callbacks) { this.responsecallbacks = callbacks; } @override protected response doinbackground(request... params) { request request = params[0]; response response = null; try { //make http call , gets response remote server here... } catch (exception e) { } return response; } protected void onpostexecute(response response) { if (response.issuccess()) { try { if (response.issuccess()) { callbacks.onsuccess(response); } } catch (exception e) { callbacks.onfailure(response); } } } }
what want update text returned server in textview after click button.
[update 2] apparently, found out if define 1 more callback uicallback , have activity register it, solves problem.
uicallback:
public interface uicallback { public void onfailure(string response); void onsuccess(string response) throws ioexception; }
have activity implement it:
public class mainactivity extends activity implements uicallback { ....... api = new netapi(); api.setuicallbacks(this); ....... @override public void onsuccess(string response) throws ioexception { string text = api.getresponsestring(); //works ! textview.settext(text); } }
does have better solution ? don't idea of activity implementing callback. have been awesome, if api deliver server response once done fetching data, api.getresponsestring().
my guess is, you're looking runonuithread.
if use in onpostexecute
, can refresh ui data without registering callbacks , whatnot. this:
protected void onpostexecute(response response) { if (response.issuccess()) { try { if (response.issuccess()) { runonuithread(new runnable() { public void run() { textview.settext(text); } }); } } catch (exception e) { callbacks.onfailure(response); } } }
a synchronous alternative using runnable in main thread. you'd have redefine mytask implement runnable
, join
after. this
thread asynctask = new thread(mytask()); asynctask.start(); asynctask.join();//gets called task ends
mytask be
public class mytask implements runnable{ public void run(){ //do whatever need } }
Comments
Post a Comment