2016-04-23 165 views
0

我想解析2 JasonArray。我不使用listview,我使用微調框和文本框來顯示數據。Android解析2 jsonarray

這是我的信息是

public class Config { 
//JSON URL 
public static final String DATA_URL = "http://bitirmeprojem.tk/data.php"; 

//Tags used in the JSON String 
public static final String TAG_USERNAME = "Ders_Ad"; 
public static final String TAG_NAME = "Ders_Ad"; 
public static final String TAG_HOCA = "Hoca_Ad"; 
//public static final String TAG_SESSION = "session"; 

//JSON array name 
public static final String JSON_ARRAY = "users"; 
public static final String JSON_ARRAY2 = "hoca"; 
} 

,這是我分析它:

private void getData(){ 
    //Creating a string request 
    StringRequest stringRequest = new StringRequest(Config.DATA_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        JSONObject j = null; 
        try { 
         //Parsing the fetched Json String to JSON Object 
         j = new JSONObject(response); 

         //Storing the Array of JSON String to our JSON Array 
         result = j.getJSONArray(Config.JSON_ARRAY); 

         //Calling method getStudents to get the students from the JSON Array 
         getStudents(result); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    //Creating a request queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

    //Adding request to the queue 
    requestQueue.add(stringRequest); 
} 

我試圖創建getData2(),但它崩潰的應用程序。

我還能做什麼?

+0

你爲什麼不分析你的陣列進一步? –

+0

@VivekMishra我沒有得到你所說的。我試圖創建新的JsonObject,並將其用於getData()中的其他數組。但它沒有奏效,它使應用程序崩潰。 –

+0

json在http://bitirmeprojem.tk/data.php無效,你可以在http://jsonviewer.stack.hu/查看。 logcat對崩潰說什麼? – Devrim

回答

0

響應試試這個JSON響應。

Setup this valid json response in your API.

{ 
    "users": [{ 
     "Ders_Ad": "E-Ticaret" 
    }, { 
     "Ders_Ad": "Yapay Zeka" 
    }, { 
     "Ders_Ad": "Mant\u0131ksal Devre Tasar\u0131m\u0131" 
    }, { 
     "Ders_Ad": "Programlama Dilleri" 
    }, { 
     "Ders_Ad": "Bilgisayar Mimarisi" 
    }, { 
     "Ders_Ad": "Veri \u0130leti\u015fimi" 
    }, { 
     "Ders_Ad": "Bilgisayar A\u011flar\u0131" 
    }, { 
     "Ders_Ad": "Veritaban\u0131 Sistemleri" 
    }], 

    "hoca": [{ 
     "Ders_Ad": "E-Ticaret" 
    }, { 
     "Ders_Ad": "Yapay Zeka" 
    }, { 
     "Ders_Ad": "Mant\u0131ksal Devre Tasar\u0131m\u0131" 
    }, { 
     "Ders_Ad": "Programlama Dilleri" 
    }, { 
     "Ders_Ad": "Bilgisayar Mimarisi" 
    }, { 
     "Ders_Ad": "Veri \u0130leti\u015fimi" 
    }, { 
     "Ders_Ad": "Bilgisayar A\u011flar\u0131" 
    }, { 
     "Ders_Ad": "Veritaban\u0131 Sistemleri" 
    }, { 
     "Hoca_Ad": "R\u0131fat", 
     "Hoca_Soyad": "Yaz\u0131c\u0131" 
    }, { 
     "Hoca_Ad": "Metin", 
     "Hoca_Soyad": "Turan" 
    }, { 
     "Hoca_Ad": "Mustafa Cem", 
     "Hoca_Soyad": "Kasapba\u015f\u0131" 
    }, { 
     "Hoca_Ad": "Mustafa Cem", 
     "Hoca_Soyad": "Kasapba\u015f\u0131" 
    }, { 
     "Hoca_Ad": "R\u0131fat", 
     "Hoca_Soyad": "Yaz\u0131c\u0131" 
    }, { 
     "Hoca_Ad": "Halim", 
     "Hoca_Soyad": "Zaim" 
    }, { 
     "Hoca_Ad": "Halim", 
     "Hoca_Soyad": "Zaim" 
    }, { 
     "Hoca_Ad": "Metin", 
     "Hoca_Soyad": "Turan" 
    }] 
} 
0

您的API可以讓你喜歡這個

{"users":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"}]}{"hoca":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"}]} 

,但實際有效的JSON格式類似這樣的

{"users":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"}], "hoca":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"}]}