使用JSON

时间:2016-06-10 15:06:30

标签: java php android mysql

我已经尝试了这里找到的所有解决方案,但似乎没有任何关系。我是android的新手,这是我的问题。

我有这个PHP脚本获取MySql数据并将其传递给数组

<?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));

执行PHP脚本时,它给了我:

  

[[{&#34; title&#34;:&#34;所有饮品40%折扣&#34;&#34; offerto&#34;&#34; 06/29   / 2016&#34;&#34;类型&#34;:&#34;销售&#34;&#34; percentageoff&#34;:&#34; 40%&#34;},{&#34;标题&#34;:&#34; 30%销售所选&gt;项目&#34;,&#34; offerto&#34;:&#34; 07/31/2016&#34;,&#34; type&#34 ;:&#34;销售&#34;&#34; percentageoff&#34;:&#34; 30%&#34;},   {&#34;标题&#34;:&#34;所选商品45%折扣&#34;,&#34; offerto&#34;:&#34; 07/08   / 2016&#34;&#34;类型&#34;:&#34;销售&#34;&#34; percentageoff&#34;:&#34; 45&#34;}]]

我的问题是当我想将此结果传递到android studio中的列表时。

这是我班上使用的代码

公共类Sale扩展AppCompatActivity实现了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);


}

}

因此,当我运行应用程序并尝试列出我从PHP脚本获取的结果时,我在android studio中得到了这个长错误,但我认为这是导致错误的主线

>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

任何帮助将不胜感激。 提前谢谢

1 个答案:

答案 0 :(得分:0)

更改行

echo json_encode(array($result));

PHP代码中的

echo json_encode($result);

$result已经是一个数组,就像你在开始时$result = array();一样。通过执行son_encode(array($result)),您将数组对象集成到另一个数组中。

在JSON响应开始时注意[[。正因为如此。在android方面,你只是反序列化外部数组。这就是为什么你得到Expected BEGIN_OBJECT but was BEGIN_ARRAY例外。