2017-07-08 2 views
0

Je suis en train de télécharger des données à partir de l'url (JSON) = https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.jsonRénovation pour afficher cette ArrayLists imbriquées

pour la mise en réseau j'ai utilisé la modernisation, et ce sont mes classes, chaque fois que je tente d'accéder aux classes imbriquées ou listes, il jette une erreur fatale.

MainActivity

public class MainActivity extends AppCompatActivity { 

ArrayList<Recipe> mRecipies = new ArrayList<>(); 
public TextView text; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    text = (TextView)findViewById(R.id.text); 
    IRecipe iRecipe = RetrofitBuilder.Retrieve(); 
    Call<ArrayList<Recipe>> recipe = iRecipe.getRecipe(); 

    recipe.enqueue(new Callback<ArrayList<Recipe>>() { 
     @Override 
     public void onResponse(Call<ArrayList<Recipe>> call, Response<ArrayList<Recipe>> response) { 
      mRecipies = response.body(); 
      text.setText(mRecipies.get(3).getIngredients().size()); 
     } 

     @Override 
     public void onFailure(Call<ArrayList<Recipe>> call, Throwable t) { 

     } 
    }); 

classe MyRecipe

public class Recipe { 

private String id; 
private String name; 
private List<Ingredients> ingredients = new ArrayList<>(); 
private List<Steps> steps = new ArrayList<>(); 
private String servings; 

public Recipe(String id, String name, String servings, List<Ingredients> ingredients, List<Steps> steps) { 
    this.id = id; 
    this.name = name; 
    this.ingredients = ingredients; 
    this.steps = steps; 
    this.servings = servings; 
} 

public Recipe(){ 

} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public List<Ingredients> getIngredients() { 
    return ingredients; 
} 

public void setIngredients(List<Ingredients> ingredients) { 
    this.ingredients = ingredients; 
} 

public List<Steps> getSteps() { 
    return steps; 
} 

public void setSteps(List<Steps> steps) { 
    this.steps = steps; 
} 

public String getServings() { 
    return servings; 
} 

public void setServings(String servings) { 
    this.servings = servings; 
} 

}

Mes ingrédients classe

public class Ingredients { 

private String quantity; 
private String measure; 
private String ingredient; 


public Ingredients(String quantity, String measure, String ingredient) { 
    this.quantity = quantity; 
    this.measure = measure; 
    this.ingredient = ingredient; 
} 

public Ingredients(){ 

} 


public String getQuantity() { 
    return quantity; 
} 

public void setQuantity(String quantity) { 
    this.quantity = quantity; 
} 

public String getMeasure() { 
    return measure; 
} 

public void setMeasure(String measure) { 
    this.measure = measure; 
} 

public String getIngredient() { 
    return ingredient; 
} 

public void setIngredient(String ingredient) { 
    this.ingredient = ingredient; 
} 

}

classes Rénovation

Builder

 public final class RetrofitBuilder { 
     static IRecipe iRecipe; 

     public static IRecipe Retrieve() { 

      Gson gson = new GsonBuilder().create(); 

      OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); 


      iRecipe = new Retrofit.Builder() 
        .baseUrl("https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/") 
        .addConverterFactory(GsonConverterFactory.create(gson)) 
        .callFactory(httpClientBuilder.build()) 
        .build().create(IRecipe.class); 


      return iRecipe; 
     } 
    } 


Interface 

    public interface IRecipe { 
    @GET("baking.json") 
    Call<ArrayList<Recipe>> getRecipe(); 
} 

Stack Trace

FATAL EXCEPTION: main 
                       Process: com.example.vamshi.retrofittrial, PID: 20350 
                       android.content.res.Resources$NotFoundException: String resource ID #0x9 
                        at android.content.res.Resources.getText(Resources.java:328) 
                        at android.content.res.MiuiResources.getText(MiuiResources.java:123) 
                        at android.widget.TextView.setText(TextView.java:4432) 
                        at com.example.vamshi.retrofittrial.MainActivity$1.onResponse(MainActivity.java:34) 
                        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70) 
                        at android.os.Handler.handleCallback(Handler.java:742) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:154) 
                        at android.app.ActivityThread.main(ActivityThread.java:5527) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 

07-08 19: 07: 00,145 20350-20350/com.example.vamshi.retrofittrial E/MQSEventManagerDelegate: Échec de Obtenez MQSService.

Répondre

1

Eh bien, n'autorise pas les nombres entiers.

Ainsi, au lieu de

text.setText(mRecipies.get(3).getIngredients().size()); 

utilisation

text.setText(String.valueof(mRecipies.get(3).getIngredients().size())); 
+1

ouais, merci qui était-ce, erreur stupide désolé –