java - Passing an array to android app with JSON -


ive tried solutions found here nothing seems work me. i'm new android , problem.

i have php script gets mysql data , passes array

<?php  $con = mysqli_connect("localhost", "username", "passwod", "database"); $result = array(); $getacceptedsotres = mysqli_query($con, "select * offer      type='sale' , approved = 'approved'"); while($rowgetid = mysqli_fetch_assoc($getacceptedsotres)){  $storeid = $rowgetid['id'];  $getstores = mysqli_query($con, "select * sale offerid = '$storeid'  ");  while($row = mysqli_fetch_assoc($getstores)){     array_push($result,array(    'title'=>$row['title'],    'offerto' => $rowgetid['offerto'],    'type' => $rowgetid['type'],    'percentageoff'=>$row['percentageoff']    ));   }  }    echo json_encode(array($result)); 

when php script executed gives me this:

[[{"title":"40% off on drinks","offerto":"06/29 /2016","type":"sale","percentageoff":"40%"},{"title":"30% sale on selected >items","offerto":"07/31/2016","type":"sale","percentageoff":"30%"}, {"title":"45% off on selected items","offerto":"07/08 /2016","type":"sale","percentageoff":"45"}]]

my problem when want pass result list in android studio.

this code used in class

public class sale extends appcompatactivity implements asyncresponse {

private arraylist<getsale> salelist; private listview lvsale;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_sale);      imageloader.getinstance().init(uilconfig.config(sale.this));      postresponseasynctask taskread = new postresponseasynctask(sale.this, this);      taskread.execute("http://....php");     }  @override public void processfinish(string s) {      salelist = new jsonconverter<getsale>().toarraylist(s, getsale.class);     binddictionary<getsale> dict = new binddictionary<getsale>();       dict.addstringfield(r.id.tvsaleamount, new stringextractor<getsale>() {         @override         public string getstringvalue(getsale getsale, int position) {             return getsale.title;         }     });      dict.addstringfield(r.id.tvtype, new stringextractor<getsale>() {         @override         public string getstringvalue(getsale getsale, int position) {             return getsale.type;         }     });      fundapter<getsale> adapter = new fundapter<>(             sale.this, salelist, r.layout.layout_sale, dict );      lvsale = (listview)findviewbyid(r.id.lvsale);     lvsale.setadapter(adapter);   } 

}

so when run application , try list result i'm getting php script long error in android studio think main line causing error

>06-10 17:34:17.262 28516-28516/com.example.w/system.err:  >com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected  >begin_object begin_array @ line 1 column 3 >06-10 17:34:17.262 28516-28516/com.example.w/system.err:     @  >com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflect>ivetypeadapterfactory.java:176) >06-10 17:34:17.262 28516-28516/com.example.w/system.err:     @  >com.google.gson.internal.bind.typeadapterruntimetypewrapper.read(typeadapterrun>timetypewrapper.java:40)  >. >. >.  >06-10 17:34:17.272 28516-28516/com.example.w/system.err:   ... 21 more >06-10 17:34:17.282 28516-28516/com.example.e/viewrootimpl: >senduseractionevent() mview == null 

any appreciated. thank in advance

change line

echo json_encode(array($result));

to

echo json_encode($result); in php code.

the $result array did $result = array(); in beginning. doing son_encode(array($result)) integrating array object other array.

notice [[ in starting of json response. it's because of that. , on android side were, deserializing outer array. that's why got expected begin_object begin_array exception.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -