2017-07-08 2 views
0

Ich versuche, Daten aus der URL (JSON) = https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.jsonRetrofit Doesnt zeigen verschachtelte Arraylisten

für die Vernetzung zum Download i Nachrüstung verwendet haben, und diese sind meine Klassen, wenn ich versuche, die verschachtelten Klassen zuzugreifen oder Listet einen schwerwiegenden Fehler auf.

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) { 

     } 
    }); 

MyRecipe Klasse

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; 
} 

}

Meine Zutaten Klasse

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; 
} 

}

Retrofit Klassen

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 20.350 bis 20.350/com.example.vamshi.retrofittrial E/MQSEventManagerDelegate: fehlgeschlagen MQSService erhalten.

Antwort

1

Nun, setText lässt keine Ganzzahlen zu.

Also statt

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

Verwendung

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

ja, Dank, dass es war, leider dummer Fehler –

Verwandte Themen