java - android json parser load more data -
because json source long, , android app crashes if takes long load, want modify code, when user scrolls down automaticaly load data or if more simple, ike add button "load more". here existing code. part must modify adding autoload or maybe "load more" button? thanks!
package com.radioxxx.aacplay; import android.app.downloadmanager; import android.app.fragment; import android.app.listfragment; import android.app.progressdialog; import android.content.context; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listadapter; import android.widget.simpleadapter; import android.widget.textview; import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedinputstream; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import java.net.url; import java.net.urlconnection; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class fazenoi extends listfragment { jsonparser2 jsonparser = new jsonparser2(); private progressdialog pdialog; arraylist<hashmap<string, string>> trackslist; jsonarray albums = null; string nume_piesa, cale; string titlu; private static final string url_albums = "http://www.radioxxx.ro/json-news"; private static final string tag_titlu = "titlu"; private static final string tag_poza = "fazenoiid"; private static final string tag_nume = "cmsdate"; private static final string tag_cale = "url"; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); trackslist = new arraylist<hashmap<string, string>>(); new loadtracks().execute(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_fazenoi, container, false); return rootview; } class loadtracks extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.setmessage("se incarca ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } protected string doinbackground(string... args) { list<namevaluepair> params = new arraylist<namevaluepair>(); //params.add(new basicnamevaluepair(tag_id, album_id)); string json = jsonparser.makehttprequest(url_albums, "get", params); log.d("track list json: ", json); try { jsonarray albums = new jsonarray(json); (int = 0; < albums.length(); i++) { jsonobject c = albums.getjsonobject(i); //string poza = c.getjsonarray("pozaprincipalamedia").getjsonobject(0).getstring("thumb"); string track_no = string.valueof(i + 1); string poza = c.getjsonobject("pozaprincipalamedia").getstring("thumb"); string idpoza = c.getjsonobject("pozaprincipalamedia").getstring("id"); string nume = c.getstring(tag_titlu); string cale = c.getstring(tag_cale); hashmap<string, string> map = new hashmap<string, string>(); map.put("track_no", track_no + "."); map.put(tag_poza, poza); map.put(tag_nume, nume); map.put(tag_cale, cale); trackslist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); getactivity().runonuithread(new runnable() { public void run() { listadapter adapter = new simpleadapter( getactivity(), trackslist, r.layout.list_faze, new string[] {tag_poza, "track_no", tag_nume }, new int[] { r.id.list_image, r.id.track_no, r.id.nume_piesa }); setlistadapter(adapter); //textview text = (textview) getactivity().findviewbyid(r.id.version); //text.settext(titlu); } }); } } }
i'm going post comment answer, believe it's easiest way solve problem. if can control how json
being sent on server side, make requests java app page 1, page 2 etc.
page 1,page 2,etc... send out small portions of json
need, attach listener (button/scrollview) request next page, on forth. hope helps!
Comments
Post a Comment