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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀