php - Display data in Android with the help of web service -
i trying display data in android of php web service, think problems code data not displayed. giving here code of both file
code of web service :
<?php $dbhandle = mysql_connect($hostname, $username, $password,$datbase_name) or die("unable connect mysql"); $selected = mysql_select_db("code_lessions",$dbhandle) or die("could not select examples"); $final_data = array(); $query="select * lessiondetail"; if ($query_run = mysql_query($query)) { $i=0; while($query_row = mysql_fetch_assoc($query_run)) { $username = $query_row['lessionname']; $password = $query_row ['categoryname']; $id = $query_row ['lessionid']; //echo $username .'`s password : '. $password.'<br>'; $data = array('lessionid'=>$id , 'lessionname'=>$username ,'categoryname'=>$password); $final_data[$i]=$data; $i++; //print(json_encode($data)); //print(json_encode($password)); } print(json_encode($final_data)); }else{ echo mysql_error(); } ?>
file : jsonexampleactivity.java
package com.json.php; import android.app.activity; import android.os.bundle; import android.widget.textview; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.hashmap; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; public class jsonexampleactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://code.guru99.com/android.php"); textview textview = (textview)findviewbyid(r.id.textview1); try { httpresponse response = httpclient.execute(httppost); string jsonresult = inputstreamtostring(response.getentity().getcontent()).tostring(); jsonobject jobject = new jsonobject(jsonresult); jsonarray json = new jsonarray(jobject.getstring(jsonresult)); arraylist<hashmap<string, string>> mylist = new arraylist<hashmap<string, string>>(); hashmap<string, string> map = new hashmap<string, string>(); for(int i=0;i<json.length();i++){ /*map = new hashmap<string, string>(); jsonobject e = json.getjsonobject(i); map.put("id", string.valueof(i)); map.put("name", "lname" + e.getstring("name")); map.put("category", "category: " + e.getstring("category")); mylist.add(map); system.out.println("the values are: " + map.values());*/ jsonobject oneobject = json.getjsonobject(i); // pulling items array string id = oneobject.getstring("id"); string name = oneobject.getstring("name"); string category = oneobject.getstring("category"); textview.settext(id + "-" + name + "-" + category); } } catch (jsonexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private jsonobject jsonobject(string jsonresult) { // todo auto-generated method stub return null; } private stringbuilder inputstreamtostring(inputstream is) { string rline = ""; stringbuilder answer = new stringbuilder(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); try { while ((rline = rd.readline()) != null) { answer.append(rline); } } catch (ioexception e) { e.printstacktrace(); } return answer; } }
there 2 records in database , want display whole data table , nothing displayed.
this not complete answer use following debug code.
httpresponse response = httpclient.execute(httppost); string jsonresult = inputstreamtostring(response.getentity().getcontent()).tostring(); log.d("jsondebug" , jsonresult);
this display input receiving in log cat. there easier determine if json in wring format or if input reader nothing @ all, think more case since post seems did not jsonparser error
Comments
Post a Comment