douyou3619 2016-06-10 15:06
浏览 80
已采纳

使用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

  • 写回答

1条回答 默认 最新

  • doujianjian2060 2016-06-10 15:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况