mr. cg mr. cg - 1 year ago 65
SQL Question

Passing an array to android app with jason

Ive tried all the solutions found here but nothing seems to work with me. I'm new to android and this is my problem.

I have this PHP script that gets MySql data and passes it into an array


$con = mysqli_connect("localhost", "username", "passwod", "database");
$result = array();
$getacceptedsotres = mysqli_query($con, "SELECT * FROM offer WHERE
type='sale' AND approved = 'approved'");
while($rowgetid = mysqli_fetch_assoc($getacceptedsotres)){

$storeid = $rowgetid['id'];

$getstores = mysqli_query($con, "SELECT * FROM sale WHERE offerid =
'$storeid' ");

while($row = mysqli_fetch_assoc($getstores)){

'offerto' => $rowgetid['offerto'],
'type' => $rowgetid['type'],



echo json_encode(array($result));

When the PHP Script is executed it gives me this:

[[{"title":"40% off on all 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

My problem is when i want to pass this result into list in android studio.

This is the Code used in my class

public class Sale extends AppCompatActivity implements AsyncResponse {

private ArrayList<GetSale> saleList;
private ListView lvSale;

protected void onCreate(Bundle savedInstanceState) {


PostResponseAsyncTask taskRead = new PostResponseAsyncTask(Sale.this, this);



public void processFinish(String s) {

saleList = new JsonConverter<GetSale>().toArrayList(s, GetSale.class);
BindDictionary<GetSale> dict = new BindDictionary<GetSale>();

dict.addStringField(, new StringExtractor<GetSale>() {
public String getStringValue(GetSale getSale, int position) {
return getSale.title;

dict.addStringField(, new StringExtractor<GetSale>() {
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(;



So when I run the application and try to list the result I'm getting from my PHP script i get this long error in android studio but i think this is the main line causing the error

>06-10 17:34:17.262 28516-28516/com.example.W/System.err:
> java.lang.IllegalStateException: Expected
>BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3
>06-10 17:34:17.262 28516-28516/com.example.W/System.err: at
>06-10 17:34:17.262 28516-28516/com.example.W/System.err: at


>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 help would be appreciated.
Thank you in advance

Answer Source

Change the line

echo json_encode(array($result));


echo json_encode($result); in your PHP code.

The $result is already an array as you did $result = array(); in the beginning. By doing son_encode(array($result)) you are integrating an array object into an other array.

Notice [[ in the starting of your JSON response. It's because of that. And on android side you were, deserializing just the outer array. So that's why you got Expected BEGIN_OBJECT but was BEGIN_ARRAY exception.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download