android - AsyncTask.execute() returns null -
i using below code version json
webservice.java
public void getsingleresponse(final string url) { class task extends asynctask<string, string, string> { @override protected string doinbackground(string... params) { // todo auto-generated method stub string resp; resp = ""; httpclient httpclient = new defaulthttpclient(); try { log.d("url", "" + params[0]); httpresponse response = httpclient.execute(new httpget( params[0])); httpentity entity = response.getentity(); log.d("entity ", "" + entity); string responsestring = entityutils.tostring(entity, "utf-8"); resp = responsestring; globalvar.version = resp; log.v("global version", "" + globalvar.version); } catch (exception e) { // todo: handle exception log.v("http:::::", "error" + e.getcause()); } return globalvar.version; } } new task().execute(url); }
mainactivity.java
public static string getmarketversion() throws jsonexception { string version = ""; try { webservice ws = new webservice(); string serverurl = server_url + "/getversion"; log.i(tag, "server url" + serverurl); ws.getsingleresponse(serverurl); string response = globalvar.version; log.v("response :: ", "version ::" + response); jsonarray jarry = new jsonarray(response); jsonobject jobj = jarry.getjsonobject(0); version = jobj.getstring("version"); log.v("parsing ", "version :: " + version); return version; } catch (exception e) { e.printstacktrace(); } return version; }
when run application got version blank see log below
07-24 12:27:42.309: v/response ::(4268): version :: 07-24 12:27:43.389: v/global version(4268): [{"version":"1.1.3"}]
but when debug application able get response
07-24 12:06:01.699: v/global version(3122): [{"version":"1.1.3"}] 07-24 12:06:07.349: v/response ::(3122): version ::[{"version":"1.1.3"}] 07-24 12:08:12.019: v/parsing(3122): version :: 1.1.3
i don't know whats going on in advance
first try understand, how asynctask works? doinbackground
works in different thread ui thread, , after execute asynctask have wait until complete, means need override onpostexecute
method of asynctask , there version, , btw should not share things using static variable in android , android provide various methods that
Comments
Post a Comment