java - AsyncTask in Android with return interface -
i'm using asynctask in android request server , recive data class. know asynctask don't return used code made other people return strings using interface.
here have asynctask class code:
public class webrequest extends asynctask<string, void, string> { //data public string mfilecontents = "false"; //api-info public webrequestresponse delegate = null; public webrequest(webrequestresponse asyncresponse) { delegate = asyncresponse;//assigning call interfacethrough constructor } @override protected string doinbackground(string... params) { mfilecontents = downloadfile(params[0]); if(mfilecontents == null) { log.d("downloaddata", "error downloading"); } return mfilecontents; } protected void oonpostexecute(string result) { super.onpostexecute(result); delegate.processfinish(mfilecontents); log.d("downloaddata", "result was: " + result); //result } private string downloadfile(string urlpath) { stringbuilder tempbuffer = new stringbuilder(); try { url url = new url(urlpath); httpurlconnection connection = (httpurlconnection) url.openconnection(); int response = connection.getresponsecode(); log.d("downloaddata", "the response code " + response); inputstream = connection.getinputstream(); inputstreamreader isr = new inputstreamreader(is); int charread; char[] inputbuffer = new char[500]; while(true){ charread = isr.read(inputbuffer); if(charread <=0) { break; } tempbuffer.append(string.copyvalueof(inputbuffer, 0, charread)); } return tempbuffer.tostring(); } catch(ioexception e) { log.d("downloaddata", "io exception reading data: " + e.getmessage()); e.printstacktrace(); } catch(securityexception e) { log.d("downloaddata", "security exception. needs permissions? " + e.getmessage()); } return null; } }
now, interface:
public interface webrequestresponse { void processfinish(string output); }
in sync class have this:
public class api implements webrequestresponse {
and make execute like:
public static void startrequest(string url) { string response = null; webrequest request = new webrequest(new webrequestresponse() { @override public void processfinish(string output) { test(output); //testing response code } }); request.execute(url); }
the problem code never called , never return anything. using log.d found nothing works here: "delegate.processfinish(mfilecontents);" allocated in asynctask class. doing wrong?
thanks
thanks rucsi, found writting onpostexecute double o
Comments
Post a Comment