2016-09-23 3 views
0

Ich möchte Daten von JSON erhalten und es dann in einem Array zurückgeben, aber das Array immer mit Null zurück, weil mein Programm nicht in die onResponse-Methode gehen wird.Warum wird die onResponse-Methode übersprungen?

Ich möchte diese Daten in einem RecyclerView zeigen. Zuerst hat es funktioniert, aber jetzt funktioniert es nicht Ich weiß nicht warum ...

private SzabadEuMusorok[] getSzabadEuMusoroks(){ 
    if (isNetworkAvaible()){ 
     OkHttpClient client = new OkHttpClient(); 
     Request request = new Request.Builder().url("validurl").build(); 

     Call call = client.newCall(request); 
     call.enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       alertUserAboutError(); 
      } 

      @Override 
      public void onResponse(Call call, Response response) throws IOException { 
       try { 
        String jsonData = response.body().string(); 
        Log.v("JSONDATA", jsonData); 
        if(response.isSuccessful()){ 
         JSONArray jsonArray = new JSONArray(jsonData); 

         mSzabadEuMusoroks = new SzabadEuMusorok[jsonArray.length()]; 

         for (int i = 0; i < jsonArray.length(); i++){ 
          JSONObject jsonObject = jsonArray.getJSONObject(i); 
          SzabadEuMusorok szabadEuMusorok = new SzabadEuMusorok(); 
          JSONArray elementTexts = jsonObject.getJSONArray("element_texts"); 

          JSONObject titleObject = elementTexts.getJSONObject(0); 
          szabadEuMusorok.setTitile(titleObject.getString("text")); 

          JSONObject subjectObject = elementTexts.getJSONObject(3); 
          szabadEuMusorok.setSubject(subjectObject.getString("text")); 

          JSONObject mainObject = jsonObject.getJSONObject("files"); 
          szabadEuMusorok.setVideoURL(mainObject.getString("url")); 

          mSzabadEuMusoroks[i] = szabadEuMusorok; 
         } 
        } 
       } catch (JSONException e) { 
        Log.e("JSONEXCEPTION", "Exception caught: ", e); 
       } 
      } 
     }); 
    } 
    else{ 
     Toast.makeText(this, R.string.network_unavaible_message, Toast.LENGTH_LONG).show(); 
    } 
    return mSzabadEuMusoroks; 
+0

ich fi durch mit Debugger gehen, es funktioniert ... – Marci

Antwort

1

Ihr Code ist im logischen Sinne falsch. Da der Aufruf des Webdienstes asynchron ist, wenn Sie also "return" verwenden, ist das Array leer, wenn Ihre Daten im Ergebnis nicht relevant sind, weil Sie das RecyclerView bereits geladen haben. Die Lösung wird sein, dass Sie eine Callback-Funktion implementieren oder notifydataset im RecyclerView geändert verwenden.

Überprüfen Sie die Links.

Using callbacks in android

Using notifydatasetchanged

---------- ---------- notifydatasetchanged

for (int i = 0; i < jsonArray.length(); i++){ 
    JSONObject jsonObject = jsonArray.getJSONObject(i); 
    SzabadEuMusorok szabadEuMusorok = new SzabadEuMusorok(); 
    JSONArray elementTexts = jsonObject.getJSONArray("element_texts"); 

    JSONObject titleObject = elementTexts.getJSONObject(0); 
    szabadEuMusorok.setTitile(titleObject.getString("text")); 

    JSONObject subjectObject = elementTexts.getJSONObject(3); 
    szabadEuMusorok.setSubject(subjectObject.getString("text")); 

    JSONObject mainObject = jsonObject.getJSONObject("files"); 
    szabadEuMusorok.setVideoURL(mainObject.getString("url")); 

    mSzabadEuMusoroks[i] = szabadEuMusorok; 
} 
//LINE ADDED----------------------------------------------------- 
yourAdapterInTheRecyclerView.notifyDataSetChanged(); 
//LINE ADDED----------------------------------------------------- 
+0

Es funktioniert immer noch nicht ... – Marci

+0

Wo sollte ich einen Rückruf()? Ich verstehe nicht... – Marci

Verwandte Themen