Friday, 15 January 2010

java - onPostExecute() getting value for last json object only -



java - onPostExecute() getting value for last json object only -

i have parsed json , getting value both objects like: image, period in asynctask, in onpostexecute() method getting value last json object only.

here getting lastly object value period :

delay, period); // getting lastly object's period value

what reason, why getting lastly object value period object in onpostexecute() method

mainactivity.java:

public class mainactivity extends activity { arraylist<string> actorslist; viewflipper viewflipper; handler mhandler ; timer timer; runnable mupdateresults; int delay = 500; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewflipper = (viewflipper) findviewbyid(r.id.flipper); actorslist = new arraylist<string>(); // execute asynctask new jsonasynctask().execute("http://localhost/images.txt"); // handler set duration , upate animation mhandler = new handler(); // create runnable posting mupdateresults = new runnable() { public void run() { viewflipper.shownext(); // shownext() method } }; timer = new timer(); } // asynctask info server class jsonasynctask extends asynctask<string, void, boolean> { int period; @override protected void onpreexecute() { super.onpreexecute(); } @override protected boolean doinbackground(string... urls) { seek { //------------------>> httpget httppost = new httpget(urls[0]); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(httppost); // statusline stat = response.getstatusline(); int status = response.getstatusline().getstatuscode(); if (status == 200) { httpentity entity = response.getentity(); string info = entityutils.tostring(entity); jsonobject jsono = new jsonobject(data); jsonarray jarray = jsono.getjsonarray("images"); (int = 0; < jarray.length(); i++) { jsonobject object = jarray.getjsonobject(i); actorslist.add(object.getstring("image")); log.d("image: ", object.getstring("image")); seek { period = integer.parseint(object.getstring("period").tostring()); log.d("period: ", object.getstring("period")); } grab (numberformatexception nfe) { // todo: handle exception system.out.print("could not parse:" + nfe); } } homecoming true; } } grab (parseexception e1) { e1.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (jsonexception e) { e.printstacktrace(); } homecoming false; } protected void onpostexecute(boolean result) { if(result == false) { toast.maketext(getapplicationcontext(), "unable fetch info server", toast.length_long).show(); } else { for(int i=0; i<actorslist.size(); i++) { imageview image = new imageview(getapplicationcontext()); picasso.with(mainactivity.this) .load(actorslist.get(i).tostring()) .placeholder(r.drawable.ic_launcher) .error(r.drawable.ic_launcher) .into(image); viewflipper.addview(image); // addview(..) method } log.d("period:post- ", string.valueof(period)); timer.scheduleatfixedrate(new timertask() { public void run() { mhandler.post(mupdateresults); } }, delay, period); // getting lastly object's period value } } } }

logcat:

10-10 11:46:52.527: d/image:(542): http://localhost/image_01.jpg 10-10 11:46:52.536: d/period:(542): 50000 10-10 11:46:52.536: d/image:(542): http://localhost/image_05.jpg 10-10 11:46:52.536: d/period:(542): 5000 10-10 11:46:52.536: d/image:(542): http://localhost/image_06.jpg 10-10 11:46:52.536: d/period:(542): 5000 10-10 11:46:52.546: d/image:(542): http://localhost/image_07.jpg 10-10 11:46:52.546: d/period:(542): 25000 10-10 11:46:52.546: d/image:(542): http://localhost/image_08.jpg 10-10 11:46:52.546: d/period:(542): 5000 10-10 11:46:52.546: d/image:(542): http://localhost/image_02.jpg 10-10 11:46:52.556: d/period:(542): 5000 10-10 11:46:52.556: d/image:(542): http://localhost/image_03.jpg 10-10 11:46:52.556: d/period:(542): 1000 10-10 11:46:52.556: d/image:(542): http://localhost/image_04.jpg 10-10 11:46:52.566: d/period:(542): 1000 10-10 11:46:52.616: d/period:post-(542): 1000

you need create arralist.

in list add together periods(like did actorslist)

mainclass add together

arraylist<integer> periods;

in oncreate()

periods=new arraylist<integer>();

in doinbackground

try { periods.add(integer.parseint(object.getstring("period").tostring())); log.d("period: ", object.getstring("period")); } grab (numberformatexception nfe) { system.out.print("could not parse:" + nfe); }

java android json

No comments:

Post a Comment