java - Android anonymous asyncTask return value in method -


now in app try http parsing url, didn't carry threads, before time...

i have such class , method:

public class twitteroauthhelper { public string httpquerytoapi(string url) {         httpget = new httpget(url);         httpparams params = new basichttpparams();         httpprotocolparams.setuseexpectcontinue(params, false);         get.setparams(params);         string response = null;         try {             sharedpreferences settings = context.getsharedpreferences("my_app", 0);             string userkey = settings.getstring("user_key", "");             string usersecret = settings.getstring("user_secret", "");             consumer.settokenwithsecret(userkey, usersecret);             consumer.sign(get);             defaulthttpclient client = new defaulthttpclient();             response = client.execute(get, new basicresponsehandler());         } catch (exception e) {             displaytoast("failed data.");         }         return response;     }  } 

and try move logic asynctask:

string result; public string httpquerytoapi(string url) {     new asynctask<string,void,string>(){          @override         protected string doinbackground(string... params) {             httpget = new httpget(string.valueof(params));             httpparams param = new basichttpparams();             httpprotocolparams.setuseexpectcontinue(param, false);             get.setparams(param);             string response = null;             try {                 sharedpreferences settings = context.getsharedpreferences("my_app", 0);                 string userkey = settings.getstring("user_key", "");                 string usersecret = settings.getstring("user_secret", "");                 consumer.settokenwithsecret(userkey, usersecret);                 consumer.sign(get);                 defaulthttpclient client = new defaulthttpclient();                 response = client.execute(get, new basicresponsehandler());             } catch (exception e) {                 displaytoast("failed data.");             }             result = response;             return response;         }     }.execute(url);     return result;     } 

but how can return response result value method?

what best practice of doing that?

do smth this:

@override protected void onpostexecute(string result) {     //result returned value doinbackground } 

if want callback antoher method should interface

public interface resultinterface {      public void resultfromhttp(string result); } 

then method

public string httpquerytoapi(string url, final resultinterface ri){       //as bove     @override     protected void onpostexecute(string result) {         if(ri!=null)             ri.resultfromhttp(result);     }  } 

implement resultinterface activity/fragment/wahtever calling httpquerytoapi


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -