java - json parser using threads, wont go into the run method android -
i new programming , making json parser, thread method not accessible, how go doing this. when debug, goes getjson method skips run method. searched on stack on flow confused threading there way this?
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(final string url) { thread t = new thread() { public void run() { looper.prepare(); //for preparing message pool child thread // making http request try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try {jobj = new jsonobject(json); } catch (jsonexception e) { // todo auto-generated catch block log.e("json parser", "error parsing data " + e.tostring()); } looper.loop(); return; } }; t.start(); return jobj; } }
it goes run()
method. principle of thread it's executed in parallel current thread. here's happens on time line:
main thread _______________________________________________________________________ | | start parser thread return jobj parser thread _________________________________________________________________ | | execute http request , parse json assign json object jobj
when return jobj
current thread, have started parser thread. maybe hasn't started executing yet. maybe it's still waiting http response. what's sure there 0% chance has finished executing , stored result in static jobj variable yet. so, value returned current thread null.
it's hard tell should do, except read more on threading , on supporting classes provided android.
Comments
Post a Comment