java - Array json cant go for my list view -
my listview not display json array titulo
. receiving following error on logcat
06-19 14:02:12.750 6497-6518/com.eu.agendamarinhagrande e/json parser﹕ error parsing data org.json.jsonexception: value  of type java.lang.string cannot converted jsonobject
the follwoing mainactivity class
package com.eu.agendamarinhagrande; import android.annotation.targetapi; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.build; import android.support.v7.app.actionbar; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; import com.eu.agendamarinhagrande.jsonparser; import com.eu.agendamarinhagrande.r; import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class mainactivity extends actionbaractivity { // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> empresalist; // url products list private static string url_all_empresas = "http://www.grifin.pt/projectoamg/conexao.php"; // json node names private static final string tag_titulo = "titulo"; // products jsonarray string resultado = null; listview lista; @targetapi(build.version_codes.honeycomb) @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // hashmap para el listview empresalist = new arraylist<hashmap<string, string>>(); new loadallproducts().execute(); // cargar los productos en el background thread lista = (listview) findviewbyid(r.id.listview); actionbar actionbar = getsupportactionbar(); actionbar.setdisplayhomeasupenabled(true); }//fin oncreate class loadallproducts extends asynctask<string, string, string> { /** * antes de empezar el background thread show progress dialog */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("a carregar eventos. por favor espere..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * obteniendo todos los productos */ protected string doinbackground(string... args) { // building parameters list params = new arraylist(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_empresas, "get", params); stringbuilder sb = new stringbuilder(); // check log cat json reponse log.d("all products: ", url_all_empresas.tostring()); resultado = sb.tostring(); try { // checking success tag // products found // getting array of products jsonarray arrayjson = new jsonarray(resultado); (int = 0; i<arrayjson.length();i++){ // storing each json item in variable jsonobject c = arrayjson.getjsonobject(i); string titulo = c.getstring(tag_titulo); // creating new hashmap hashmap map = new hashmap(); // adding each child node hashmap key => value map.put(tag_titulo, titulo); empresalist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( mainactivity.this, empresalist, r.layout.single_post, new string[]{ tag_titulo }, new int[]{ r.id.single_post_tv_id }); // updating listview //setlistadapter(adapter); lista.setadapter(adapter); } }); } } }
please me understand problem causing json not displayed on listview.
this worked me. commented out code make work,
public class test extends actionbaractivity { // progress dialog private progressdialog pdialog; // creating json parser object // jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> empresalist; // url products list private static string url_all_empresas = "http://www.grifin.pt/projectoamg/conexao.php"; // json node names private static final string tag_titulo = "titulo"; // products jsonarray string resultado = null; listview lista; @targetapi(build.version_codes.honeycomb) @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_test); // hashmap para el listview empresalist = new arraylist<hashmap<string, string>>(); new download().execute(); // cargar los productos en el background thread lista = (listview) findviewbyid(r.id.listview); // actionbar actionbar = getsupportactionbar(); // actionbar.setdisplayhomeasupenabled(true); }//fin oncreate public class download extends asynctask<void, void, string> { @override protected string doinbackground(void... params) { string out = null; try { defaulthttpclient httpclient = new defaulthttpclient(); final httpparams httpparameters = httpclient.getparams(); httpconnectionparams.setconnectiontimeout(httpparameters, 15000); httpconnectionparams.setsotimeout(httpparameters, 15000); httpget httppost = new httpget(url_all_empresas); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); out = entityutils.tostring(httpentity, http.utf_8); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return out; } @override protected void onpostexecute(string result) { super.onpostexecute(result); arraylist<string> list = new arraylist<>(); try { jsonarray jsonarray = new jsonarray(result); (int = 0; < jsonarray.length(); i++) { jsonobject jsa = jsonarray.getjsonobject(i); string str = jsa.getstring("titulo"); log.e("tag", str); string s1 = normalizer.normalize(str, normalizer.form.nfkd); string regex = pattern.quote("[\\p{incombiningdiacriticalmarks}\\p{islm}\\p{issk}]+"); str = new string(s1.replaceall(regex, "").getbytes("ascii"), "ascii"); list.add(str); } arrayadapter adapter = new arrayadapter<>(test.this, android.r.layout.simple_list_item_1, list); // updating listview //setlistadapter(adapter); lista.setadapter(adapter); } catch (exception e) { e.printstacktrace(); } } } }
Comments
Post a Comment