java - How to retrieve data from json array and display in textview? -
java - How to retrieve data from json array and display in textview? -
i've been using coding illustration link: http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/ code shown below:
public class nutrient extends listactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); string result = null; inputstream = null; stringbuilder sb=null; string result=null; textview fdi = (textview)findviewbyid(r.id.textview1); textview fdn = (textview)findviewbyid(r.id.textview2); //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://127.0.0.1/food.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection"+e.tostring()); } //convert response string try{ bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); sb = new stringbuilder(); sb.append(reader.readline() + "\n"); string line="0"; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //paring info int fd_id; string fd_name; try{ jarray = new jsonarray(result); jsonobject json_data=null; for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); fd_id=json_data.getint("food_id"); fd_name=json_data.getstring("food_name"); } }catch(jsonexception e1){ toast.maketext(getbasecontext(), "no nutrient found", toast.length_long).show(); }catch (parseexception e1){ e1.printstacktrace(); } } } now question how pass info list view, each iteration list item must added retrieved data.
please help me
thanks in advance
you getting strings in below code, why don't utilize that.
for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); fd_id=json_data.getint("food_id"); fd_name=json_data.getstring("food_name"); } //fdname , fd_id getting right,
if code not working add together problem facing.
according response/result server below code working me , giving output in string ,
seek { string response ="[{\"food_id\":\"1\",\"food_name\":\"rice\"},{\"food_id\":\"2\",\"food_name\":\"daal\"}] "; jsonarray array; array = new jsonarray(response); (int = 0; < array.length(); i++) { jsonobject obj = array.getjsonobject(0); string id_fd = obj.getstring("food_id"); string name_fd = obj.getstring("food_name"); log.d("jsonarray", id_fd+" " +name_fd); }} grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } please utilize according need.
java android json
Comments
Post a Comment