2017-07-16 63 views
-2

引起:org.json.JSONException:值 {「Description」:「sd」,「Title_Image」:「pic5.png」,「Vedio_Title」:「sometitle」, 「S_category_Id」: 「1」,「}類型org.json.JSONObject的 不能轉換到JSONArray我在使用andriod Volley獲取Jsone數據時出現此錯誤。我不知道錯誤在哪裏

我的Android代碼是

public class ShowData extends AppCompatActivity { 
private String url="http://192.168.2.10/Library/A/ShowAllData.php"; 
    private ProgressDialog dialog; 
    private List<item> list=new ArrayList<item>(); 
    private ListView listView; 
    private itemAdapter adapter;  
public class ShowData extends AppCompatActivity { 
private String url="http://192.168.2.10/Library/A/ShowAllData.php"; 
    private ProgressDialog dialog; 
    private List<item> list=new ArrayList<item>(); 
    private ListView listView; 
    private itemAdapter adapter; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.showdata); 
listView= (ListView) findViewById(R.id.list_item); 
     adapter=new itemAdapter(this,list); 
     listView.setAdapter(adapter); 
     dialog=new ProgressDialog(this); 
     dialog.setMessage("Loading....."); 
     Log.d("I Am near ","to jsonarray"); 


     JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
      // hide(); 


       ////////get json data 
       for(int i=0; i<response.length(); i++){ 

        try { 
         JSONObject obj=response.getJSONObject(i); 
         item item=new item(); 
         item.setTitlwimage(obj.getString("Title_Image")); 
         item.setTitle(obj.getString("Ved_Title")); 
         item.setDescription(obj.getString("Description")); 
         item.setCate(obj.getInt("S_category_Id")); 
         //item.setTime(obj.getString()); 
         Log.d("I Am near ","to loop"); 
        list.add(item); 
        } 
        catch (JSONException e) { 
         e.printStackTrace(); 
         Log.d("Error"," and "+String.valueOf(response)); 
       } 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
        error.printStackTrace(); 
      } 
     }); 

     Singleton.getinstance().addrequest(jsonArrayRequest); 
    } 
    public void hide(){ 
     if(dialog!=null){ 

      dialog.dismiss(); 
      dialog=null; 
     } 
    } 

回答

0

{"Description":"sd","Title_Image":"pic5.png","Vedio_Title":"sometitle","S_category_Id":"1","}是本身其在response通過的JSONObject我假設你不能撥打getJSONObjectJSONObject本身。

嘗試將{"Description":"sd","Title_Image":"pic5.png","Vedio_Title":"sometitle","S_category_Id":"1","}放入JSON數組(看起來像[{"Description":"sd","Title_Image":"pic5.png","Vedio_Title":"sometitle","S_category_Id":"1","}]),無論您在哪裏發出響應。

+1

謝謝你解決我的問題。我在php文件 –

+0

錯誤很高興幫助。如果問題解決了,請點擊該打勾按鈕。 – EdmDroid

相關問題