2016-06-10 20 views
0

我嘗試了所有在此處找到的解決方案,但似乎沒有任何與我一起工作。我是Android新手,這是我的問題。將數組傳遞給帶有JSON的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腳本執行的時候給了我這樣的:

[[{ 「稱號」: 「所有飲料40%優惠」,「有效」:「06/29 /2016」,「type」:「sale」,「percentageoff」:「40%」},{「title」:「30% selected> items「,」offerto「:」07/31/2016「,」type「:」sale「,」percentageoff「:」30%「}, {」title「:」45%off on selected items「, 「offerto」:「07/08 /2016」,「type」:「sale 「,」percentageoff「:」45「}]]

我的問題是當我想要將此結果傳遞到android studio中的列表。

這是

公共類銷售擴展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); 


} 

}

所以,當我運行應用程序並嘗試列出的結果我在我的課堂上使用的代碼m從我的PHP腳本得到我在Android工作室得到這個長的錯誤,但我認爲這是導致錯誤的主線

>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

在您的PHP代碼中將'echo json_encode(array($ result));'改爲'echo json_encode($ result);'。 –

+0

@Prera​​kSola omg它的工作..我有一個不同的數組類型,它不與echo json_encode($結果);但我改變了陣列,忘了再次嘗試這個回聲謝謝 –

+0

高興地幫助.. :) –

回答

0

變化在你的PHP代碼行

echo json_encode(array($result));

echo json_encode($result);

$result已經是一個數組了,就像您在開始時一樣使用了$result = array();。通過執行son_encode(array($result))您正在將一個數組對象集成到另一個數組中。

請注意[[在您的JSON響應開始。正因爲如此。而在Android的一面,你只是反序列化外部數組。所以這就是爲什麼你得到了Expected BEGIN_OBJECT but was BEGIN_ARRAY例外。

相關問題