android - How to get a string back from AsyncTask? -


i have following class:

public class geturldata extends asynctask<string, integer, string>{  @override protected string doinbackground(string... params) {     string line;     try {           defaulthttpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(params[0]);          httpresponse httpresponse = httpclient.execute(httppost);         httpentity httpentity = httpresponse.getentity();         line = entityutils.tostring(httpentity);      } catch (unsupportedencodingexception e) {         line = "<results status=\"error\"><msg>can't connect server</msg></results>";     } catch (malformedurlexception e) {         line = "<results status=\"error\"><msg>can't connect server</msg></results>";     } catch (ioexception e) {         line = "<results status=\"error\"><msg>can't connect server</msg></results>";     }     return line; }  @override protected void onpostexecute(string result) {     super.onpostexecute(result); }  } 

and trying call this:

string output = null; output = new geturldata().execute("http://www.domain.com/call.php?locationsearched=" + locationsearched); 

but output variable isn't getting data, instead getting error:

type mismatch: cannot convert asynctask<string,integer,string> string 

the method execute returns aynsctask itself, need call get:

output =     new geturldata()         .execute("http://www.example.com/call.php?locationsearched=" + locationsearched)         .get(); 

this start new thread (via execute) while blocking current thread (via get) until work new thread has been finished , result has been returned.

if this, turned async task sync one.

however, problem using get because blocks, needs called on worker thread. however, asynctask.execute() needs called on main thread. although code work, may undesired results. suspect get() under-tested google, , possible introduced bug somewhere along line.

reference: asynctask.get


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -