php - org.json.JSONException: No value for item -


i no value {"username":"sara"}{"username":"john"} jsonexception when attempt view data mysql database using android app.

  08-20 04:26:39.396: w/system.err(4732): org.json.jsonexception: no value {"username":"sara"}{"username":"john"}   08-20 04:26:39.497: w/system.err(4732):   @ org.json.jsonobject.get(jsonobject.java:354)   08-20 04:26:39.497: w/system.err(4732):   @ org.json.jsonobject.getjsonarray(jsonobject.java:544)   08-20 04:26:39.517: w/system.err(4732):   @ com.example.phpapp.viewdata$mytask.doinbackground(viewdata.java:69)   08-20 04:26:39.577: w/system.err(4732):   @ com.example.phpapp.viewdata$mytask.doinbackground(viewdata.java:1)   08-20 04:26:39.606: w/system.err(4732):   @ android.os.asynctask$2.call(asynctask.java:287)   08-20 04:26:39.606: w/system.err(4732):   @ java.util.concurrent.futuretask.run(futuretask.java:234)   08-20 04:26:39.631: w/system.err(4732):   @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)   08-20 04:26:39.636: w/system.err(4732):   @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080)   08-20 04:26:39.636: w/system.err(4732):   @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573)   08-20 04:26:39.636: w/system.err(4732):   @ java.lang.thread.run(thread.java:856) 

php code

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("can't connect"); $db = mysql_select_db($dbname) or die("no such db name"); $query = "select username users"; $result = mysql_query($query); while($row=  mysql_fetch_array($result)){     echo json_encode($row); } 

jsonarray

{"username":"sara"}{"username":"john"} 

android code

httpresponse response = client.execute(post); httpentity entity = response.getentity(); inputstream = entity.getcontent(); bufferedreader br = new bufferedreader(new inputstreamreader(is));  string str = ""; while((str = br.readline()) != null){     s.append(str+"\n");     mystr = s.tostring(); }     jsonobject object = new jsonobject(mystr); jsonarray jarray = object.getjsonarray(mystr); (int = 0; < jarray.length(); i++) {     jsonobject jasonobject = jarray.getjsonobject(i);     st = jasonobject.getstring("username");     alist.add(str); } 

how solve this? thanks

problem in line

 jsonarray jarray = object.getjsonarray(mystr); 

where mystr={"username":"sara"}{"username":"john"} (the data input stream)

so here trying access json array index mystr jsonobject object

which technically never exist giving jsonexception: no value {"username":"sara"}{"username":"john"}

solution

correct php code using jjpa's answer then

use

jsonarray jarray=new jsonarray(mystr); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -