使用JSON将数组传递给Android应用程序

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

<?php

$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)){

   array_push($result,array(
   'title'=>$row['title'],
   'offerto' => $rowgetid['offerto'],
   'type' => $rowgetid['type'],
   'percentageoff'=>$row['percentageoff']

  ));

 }
 }

  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 /2016","type":"sale","percentageoff":"45"}]]

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;

@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 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: 
>com.google.gson.JsonSyntaxException: 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 
>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:     at 
>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 help would be appreciated. Thank you in advance

Change the line

echo json_encode(array($result));

to

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.