java - Android JSON Parsing issue: seems to seperate it into 2 seperate objects and only recognizes one -
what does: android app going mimic site team creating, apdata.info. second page, display th airpoprts fit search requested.
php
...
$sql = "select apname,apcity,apstate,apcountry,iata airports match(apname,apcity,apstate,apcountry,iata) against (? in boolean mode)"; $link = connecttodb(); if($stmt = $link->prepare($sql)) { $stmt->bind_param('s', $search); if($stmt->execute()) { $stmt->store_result(); if($stmt->num_rows >= 1) { $resultset = array(); $stmt->bind_result($apname, $apcity, $apstate, $apcountry, $iata); $search_rows = $stmt->num_rows; if($search_rows > 0){ while($stmt->fetch()) { $resultset[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'iata' => $iata); //echo "your search returned $search_rows results"; //echo "<a href='/airports/airport.php?iata=".$iata."'>".$apname."</a><br>"; print(json_encode($resultset)); ...
java :
private void fetchresults(string searchfor){ string result = ""; //the year data send arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("search", searchfor)); //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://apdata.info/results/androidresult.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); entity = response.getentity(); = entity.getcontent(); //system.out.println("------------looks here"); }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); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); //system.out.println(result); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } system.out.println(result); //parse json data try{ jsonarray jarray = new jsonarray(result); system.out.println(); results = new apresult[jarray.length()]; for(int i=0;i<jarray.length();i++){ system.out.println("-----------jarray.length"); jsonobject json_data = jarray.getjsonobject(i); results[i] = new apresult( json_data.getstring("iata"), json_data.getstring("apname"), json_data.getstring("apcity"), json_data.getstring("apstate"), json_data.getstring("apcountry") ); } }catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); } }
result result of printing out result string:
03-13 07:40:06.985: i/system.out(220): [{"apname":"bismarck municipal airport","apcity":"bismarck","apstate":"north dakota","apcountry":"united states","iata":"bis"}][{"apname":"bismarck municipal airport","apcity":"bismarck","apstate":"north dakota","apcountry":"united states","iata":"bis"},{"apname":"hector international airport","apcity":"fargo","apstate":"north dakota","apcountry":"united states","iata":"far"}][{"apname":"bismarck municipal airport","apcity":"bismarck","apstate":"north dakota","apcountry":"united states","iata":"bis"},{"apname":"hector international airport","apcity":"fargo","apstate":"north dakota","apcountry":"united states","iata":"far"},{"apname":"cologne bonn airport","apcity":"cologne \/ bonn","apstate":"north rhine-westphalia","apcountry":"germany","iata":"cgn"}][{"apname":"bismarck municipal airport","apcity":"bismarck","apstate":"north dakota","apcountry":"united states","iata":"bis"},{"apname":"hector international airport","apcity":"fargo","apstate":"north dakota","apcountry":"united states","iata":"far"},{"apname":"cologne bonn airport","apcity":"cologne \/ bonn","apstate":"north rhine-westphalia","apcountry":"germany","iata":"cgn"}, {"apname":"dusseldorf international airport","apcity":"dusseldorf","apstate":"north rhine-westphalia","apcountry":"germany","iata":"dus"}]
the thing is, jarray.length = 1, tho received many more that. , can see form output, string seems separated 2 sections, first [{"apname":"bismarck municipal airport","apcity":"bismarck","apstate":"north dakota","apcountry":"united states","iata":"bis"}] while others separated comas.
to fix issue added function fixed bug:
private string fixbug(string result) { result = result.replace("][", ","); return result; }
but know why occurred in first place.
you should not make seperate json per result. should contain results in 1 json. save data in variable, , outside of while loop can encode valid json. try using http://jsonlint.com/ validation.
right have
while() { $resultset[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'iata' => $iata); print(json_encode($resultset)); }
which should be
while() { $resultset[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'iata' => $iata); } print(json_encode($resultset));
Comments
Post a Comment