java - How would I implement my present threads into an AsyncTask to make the UI respond better? -
for activities work, have info internet.
i implemented threads, not java or threads yet, kinda rushed through hoping work. works, ui feels slow because takes awhile until activity appears.
here code
an activity, lets phone call mainactivity calls:
jsonobject info = webapi.getdata(id);
the class webapi pieces url together:
public static jsonobject getdata(string id) { string url = url; url += data_url; url += value_data_id + id; homecoming webinterface.executeweb(url); }
and hands on webinterface, in webinterface whole thing gets executed:
public static string geturl(final string url) { final stringbuilder sb = new stringbuilder(); thread thread = new thread(new runnable() { public void run() { seek { inputstream = (inputstream) new url(url).getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); string result, line = reader.readline(); result = line; while((line=reader.readline())!=null){ result+=line; } sb.append(result); } grab (exception e) { // todo: handle exception e.printstacktrace(); } } }); thread.start(); seek { thread.join(); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } string result = sb.tostring(); log.d(app.tag, result); homecoming result; } public static jsonobject executeweb(final string url) { string result = webinterface.geturl(url); jsonobject json = null; seek { json = new jsonobject(result.trim()); } grab (jsonexception e) { seek { json = new jsonobject("{}"); } grab (jsonexception e1) { // todo auto-generated grab block e1.printstacktrace(); } } homecoming json; }
well works sense work better, if implement asynctask. activity show cached info until "real" info appears. possible asynctask? , need redo lot of code that?
thanks in advance help!
edit:
thanks whoever suggested asynctaskloader (i think 1 deleted answer) did asynctaskloader, handy , easy!
in effect code single threaded because of thread.join(). current thread waits newly spawned thread finish. have in effect not removed network main thread, hence ui "locks up" or feels slow.
you have callback runs on main thread when thread finished, asynctask you, , allows update progress bar if desired. read documentation on how implement asynctask.
java android multithreading android-asynctask
No comments:
Post a Comment