android - Image Auto Slider using JSON -
making image auto slider
, have written code to parse json
still using drawable
images
actually concern
, how utilize online images
in slideshow instead of drawables
please clear
me because if same kind
of problem occurs in future able make changes
.
check code below:
public class mainactivity extends activity { arraylist<actors> actorslist; public int currentimageindex = 0; imageview slidingimage; // using drawable images // in place of these static images, how can utilize arraylist // along downloadimagetask class, have written posted below private int[] image_ids = { r.drawable.splash0, r.drawable.splash1, r.drawable.splash2, r.drawable.splash3 }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actorslist = new arraylist<actors>(); // execute asynctask new jsonasynctask().execute("http://microblogging.wingnity.com/jsonparsingtutorial/jsonactors"); // handler set duration , upate animation final handler mhandler = new handler(); // create runnable posting final runnable mupdateresults = new runnable() { public void run() { animateandslideshow(); } }; int delay = 0000; int period = 4000; timer timer = new timer(); timer.scheduleatfixedrate(new timertask() { public void run() { mhandler.post(mupdateresults); } }, delay, period); } // asynctask info server class jsonasynctask extends asynctask<string, void, boolean> { @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("actors"); (int = 0; < jarray.length(); i++) { jsonobject object = jarray.getjsonobject(i); actors actor = new actors(); actor.setimage(object.getstring("image")); log.d("image: ", object.getstring("image")); actorslist.add(actor); } 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(); } } // method show slide show private void animateandslideshow() { slidingimage = (imageview)findviewbyid(r.id.imageview3_left); slidingimage.setimageresource(image_ids[currentimageindex%image_ids.length]); currentimageindex++; } // method dowload online images private class downloadimagetask extends asynctask<string, void, bitmap> { imageview bmimage; public downloadimagetask(imageview bmimage) { this.bmimage = bmimage; } protected bitmap doinbackground(string... urls) { string urldisplay = urls[0]; bitmap micon11 = null; seek { inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in); } grab (exception e) { log.e("error", e.getmessage()); e.printstacktrace(); } homecoming micon11; } protected void onpostexecute(bitmap result) { bmimage.setimagebitmap(result); } } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <imageview android:id="@+id/imageview3_left" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" /> </relativelayout>
logcat:
10-09 05:21:34.636: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/brad.jpg 10-09 05:21:34.636: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/cruise.jpg 10-09 05:21:34.636: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/johnny.jpg 10-09 05:21:34.636: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/jolie.jpg 10-09 05:21:34.666: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/tom.jpg 10-09 05:21:34.666: d/image:(735): http://microblogging.wingnity.com/jsonparsingtutorial/will.jpg
here code. utilize view flipper
public class mainactivity extends activity { arraylist<string> actorslist; viewflipper viewflipper; @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://microblogging.wingnity.com/jsonparsingtutorial/jsonactors"); // handler set duration , upate animation final handler mhandler = new handler(); // create runnable posting final runnable mupdateresults = new runnable() { public void run() { animateandslideshow(); } }; int delay = 500; int period = 4000; timer timer = new timer(); timer.scheduleatfixedrate(new timertask() { public void run() { mhandler.post(mupdateresults); } }, delay, period); } // asynctask info server class jsonasynctask extends asynctask<string, void, boolean> { progressdialog dialog; @override protected void onpreexecute() { super.onpreexecute(); dialog = new progressdialog(mainactivity.this); dialog.setmessage("loading, please wait"); dialog.settitle("connecting server"); dialog.show(); dialog.setcancelable(false); } @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("actors"); (int = 0; < jarray.length(); i++) { jsonobject object = jarray.getjsonobject(i); // actors actor = new actors(); actorslist.add(object.getstring("image")); // actor.setimage(object.getstring("image")); log.d("image: ", object.getstring("image")); // actorslist.add(actor); } homecoming true; } //------------------>> } grab (parseexception e1) { e1.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (jsonexception e) { e.printstacktrace(); } homecoming false; } protected void onpostexecute(boolean result) { dialog.cancel(); if(result == false){ toast.maketext(getapplicationcontext(), "unable fetch info server", toast.length_long).show(); } else{ setflipperimage( actorslist); } } } private void setflipperimage(arraylist<string> actorslist) { for(int i=0;i<actorslist.size();i++){ log.i("set filpper called", actorslist.get(i).tostring()+""); imageview image = new imageview(getapplicationcontext()); // image.setbackgroundresource(res); picasso.with(mainactivity.this) .load(actorslist.get(i).tostring()) .placeholder(r.drawable.ic_launcher) .error(r.drawable.ic_launcher) .into(image); viewflipper.addview(image); } } // method show slide show private void animateandslideshow() { viewflipper.shownext(); }}
and utilize picasso load images....
android json
No comments:
Post a Comment