android parsing using php genrated JSON -


i have created php script , pick java im having trouble converting format can use.

php

<?php   //pdo extension  defines lightweight, consistent interface accessing databases in php.   $db=new pdo('mysql:dbname=mydb;host=localhost;','root','');   //here prepare query analyzing, prepared statements use less resources , run faster   $row=$db->prepare('select * drinks');    $row->execute();//execute query   $json_data=array();//create array   foreach($row $rec)//foreach loop   {   $json_array['drinks_id']=$rec['drinks_id'];       $json_array['drink_name']=$rec['drink_name'];       $json_array['description']=$rec['description'];     //here pushing values in array       array_push($json_data,$json_array);    }    //built in php function encode data in json format   //print_r($json_array); echo json_encode($json_data,json_force_object);    ?>   

json sample(each row accumative instead of being under certian tag name)

{    "0":{       "drinks_id":"1",       "drink_name":"uprising treason west coast ipa",       "description":"beer"    },    "1":{       "drinks_id":"2",       "drink_name":"flying dog snake dog ipa",       "description":"beer"    },    "2":{       "drinks_id":"3",       "drink_name":"crafty dan 13 guns america ipa",       "description":"beer"    },    "3":{       "drinks_id":"4",       "drink_name":"sixpoint resin double ipa",       "description":"beer"    },    "4":{       "drinks_id":"5",       "drink_name":"sixpoint bengali ipa",       "description":"beer"    },    "5":{       "drinks_id":"6",       "drink_name":"shipyard ",       "description":"beer"    },    "6":{       "drinks_id":"7",       "drink_name":"blue moon belgian white ",       "description":"beer"    },    "7":{       "drinks_id":"8",       "drink_name":"brewdog punk ipa ",       "description":"beer"    },    "8":{       "drinks_id":"9",       "drink_name":"lagunitas ipa",       "description":"beer"    },    "9":{       "drinks_id":"10",       "drink_name":"brooklyn larger ",       "description":"larger"    },    "10":{       "drinks_id":"11",       "drink_name":"hazy hog cloudy english cider ",       "description":"cider" } } 

method in android

private void parsejson() {         defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams());         httppost httppost = new httppost(url); // depends on web service         httppost.setheader("content-type", "application/json");      inputstream inputstream = null;     string result = null;     try {         httpresponse response = httpclient.execute(httppost);         httpentity entity = response.getentity();          inputstream = entity.getcontent();         // json utf-8 default         bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);         stringbuilder sb = new stringbuilder();         log.i(tag, "readline : "+reader.readline());            string line = null;         while ((line = reader.readline()) != null)         {             sb.append(line + "\n");             log.i(tag, "printline: "+line);         }         result = sb.tostring();         jsonobject jobject = new jsonobject(result);         jsonarray jarray = jobject.getjsonarray("0"); //i belive error unclear how fix         (int i=0;i < jarray.length(); i++)         {             try {                 jsonobject oneobject = jarray.getjsonobject(i);                 // pulling items array                 int oneobjectsitem = oneobject.getint("drinks_id");                 string oneobjectsitem2 = oneobject.getstring("drink_name");                 string oneobjectsitem3 = oneobject.getstring("description");                 log.i(tag, "parsejson: "+oneobjectsitem2);             } catch (jsonexception e) {                 log.e(tag, "parsejson1: " +e.getmessage() );             }         }     } catch (exception e) {         log.e(tag, "parsejson2: "+e.getmessage() );     }     {         try{             if(inputstream != null)                 inputstream.close();}catch(exception squish){             log.e(tag, "parsejson3: "+squish.getmessage() );         }     } } 

log cat sample (only 1 line printed?)

e/menu: parsejson2: value {"drinks_id":"1","drink_name":"uprising treason west coast ipa","description":"beer"} @ 0 of type org.json.jsonobject cannot converted jsonarray 

try way creating associative array in php,

<?php    $object=array();    $i=0;    while($i<10){      $employee = array("name"=>"hassan", "designation"=>"software engineer");      $i++;      $object[]=$employee;    }    $orginal["data"]=$object;    echo json_encode($orginal) ?> 

output be

{"data":[{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"},{"name":"hassan","designation":"software engineer"}]} 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -