2017-05-10 107 views
0

我想解析從服務器獲取的JSON響應,我收到錯誤消息:JSONArray無法轉換爲JSONObject。json- JSONarray無法轉換爲JSONObject

org.json_JSONEXCEPTION: Value [{}] at 0 of type org.json.JSONArray cannot be converted to JSONObject 





try { 

      JSONArray jArray = new JSONArray(result); 
      for(int i=0;i<jArray.length();i++){ 
       JSONObject json_data = jArray.getJSONObject(i); 

       DataFood foodData = new DataFood(); 

       foodData.foodImage= json_data.getString("gambar"); 
       foodData.foodName= json_data.getString("nama"); 
       foodData.foodId= json_data.getInt("id"); 
       foodData.price= json_data.getInt("harga"); 
       data.add(foodData); 
      } 

這是我的PHP code`

<?php 
require "connection.php"; 
$sql = "select * from main"; 

$res = mysqli_query($con,$sql); 

$result = array(); 

while($row = mysqli_fetch_array($res)){ 
array_push($result, 
array('id'=>$row[0], 
'name'=>$row[1], 
'harga'=>$row[2], 
'gambar'=>$row[3] 
)); 
} 

echo json_encode(array($result)); 

mysqli_close($con); 

?> 

這是我的響應串this is my response string, i can't type here because it's too long

如何解決這個問題呢?我嘗試了很多方法,但找不到任何解決方案。

error Logcat 

    05-10 03:47:08.600 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/RecyclerView: No adapter attached; skipping layout 
05-10 03:47:08.900 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/RecyclerView: No adapter attached; skipping layout 
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo W/ResourceType: No package identifier when getting value for resource number 0x00000001 
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo D/AndroidRuntime: Shutting down VM 
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9cd5db20) 
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/AndroidRuntime: FATAL EXCEPTION: main 
                          Process: foodsqlitedemo.quocnguyen.com.foodsqlitedemo, PID: 8806 
                          android.content.res.Resources$NotFoundException: String resource ID #0x1 
                           at android.content.res.Resources.getText(Resources.java:244) 
                           at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
                           at android.widget.TextView.setText(TextView.java:3888) 
                           at com.kelompok3.restaurantapp.restoranfixx.Adapter.AdapterFood.onBindViewHolder(AdapterFood.java:50) 
                           at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6067) 
                           at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6100) 
                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5282) 
                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158) 
                           at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061) 
                           at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445) 
                           at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408) 
                           at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580) 
                           at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379) 
                           at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188) 
                           at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                           at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660) 
                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1436) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                           at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                           at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                           at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                           at android.view.View.layout(View.java:14817) 
                           at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                           at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987) 
                           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744) 
                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
                           at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
                           at android.view.Choreographer.doFrame(Choreographer.java:544) 
                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
                           at android.os.Handler.handleCallback(Handler.java:733) 
                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                           at android.os.Looper.loop(Looper.java:136) 
                           at android.app.ActivityThread.main(ActivityThread.java:5017) 
                           at java.lang.reflect.Method.invokeNative(Native Method) 
                           at java.lang.reflect.Method.invoke(Method.java:515) 
                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                           at com.android.i 
+0

你是什麼結果字符串中可以看到? –

+0

請提供您的答覆字符串。 –

回答

0

更改線路

JSONObject json_data = jArray.getJSONObject(i); 

JSONArray innerArray = jArray.getJSONArray(i); 

和遍歷JSONArray一次。

for (int i = 0; i < innerArray.length(); i++) { 
    JSONObject json_data = innerArray.getJSONObject(i); 
    DataFood foodData = new DataFood(); 

    foodData.foodImage= json_data.getString("gambar"); 
    foodData.foodName= json_data.getString("name"); 
    foodData.foodId= json_data.getInt("id"); 
    foodData.price= json_data.getInt("harga"); 
    data.add(foodData); 
} 

你的外JSONArray包含JSONArrays,而不是一個JSONObjects可以在你的PHP代碼

$result = array(); <-- your main array 
while($row = mysqli_fetch_array($res)){ 
array_push($result, <-- you add a new array to your main array 
array('id'=>$row[0], 
'name'=>$row[1], 
'harga'=>$row[2], 
'gambar'=>$row[3] 
)); 
} 
+0

我必須改變這一行嗎? JSONArray jArray = new JSONArray(result); – Fawwaz

+0

不,該行可以留 – Denny

+0

我遵循你的建議,但它說「無法解決方法的大小()」對不起,先生,我是新手 – Fawwaz

相關問題