android error when trying to put json data in viewlist php+json+java -


i trying make search page andriod app using php,json , sql.

here code giving me error :

  try{         jsonarray jarray = new jsonarray(result);         int jarrleng = jarray.length();         for(int i=0; i<jarrleng;i++){             jsonobject json_data= jarray.getjsonobject(i);              tempid += json_data.getstring("id") + "\n";             tempid += json_data.getstring("heading") + "\n";             tempid += json_data.getstring("rank") + "\n:";          }          arr = tempid.split(":");         resultlv.setadapter(new arrayadapter<string>(searchpage.this, android.r.layout.simple_list_item_1,arr));        }catch (exception e){         string errmsg = "error when putting json data in list";         toast.maketext(getapplicationcontext(), errmsg, toast.length_long).show();     } 

i have used code on page , works when using page/activity gives me error when trying put json data in listview.

i guess problem when setting arrayadapter, have done wrong?

resultlv.setadapter(new arrayadapter<string>(searchpage.this, android.r.layout.simple_list_item_1,arr)); 

to answer final question:

    //array list      list<string> your_array_list = new arraylist<string>();      try{     jsonarray jarray = new jsonarray(result);     int jarrleng = jarray.length();     for(int i=0; i<jarrleng;i++){         jsonobject json_data= jarray.getjsonobject(i);          your_array_list.add(json_data.getstring("id") + "\n");         your_array_list.add(json_data.getstring("heading") + "\n");         your_array_list.add(json_data.getstring("rank") + "\n:");        }     resultlv.setadapter(new arrayadapter<string>(searchpage.this, android.r.layout.simple_list_item_1,your_array_list)); 

    //then, items inside adapter:     for(string item_in_list : your_array_list){         system.out.println(item_in_list);     } 

to answer original question:

check out http://jsonformatter.curiousconcept.com/ formatting issues

try looking @ info: http://www.json.org/javadoc/org/json/jsonobject.html#jsonobject%28java.lang.string%29

jsonobject

public jsonobject(java.lang.string source)            throws jsonexception 

construct jsonobject source json text string. commonly used` jsonobject constructor.

parameters:     source - `a string beginning { (left brace) , ending } (right brace).`  throws:     jsonexception - if there syntax error in source string or duplicated key. 

fyi: inbuilt jsonobject , jsonarray cannot used json responses.

you try downloading small library named "json-simple-1.1.1.jar" link https://json-simple.googlecode.com/files/json-simple-1.1.1.jar.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -