Retrofit不显示嵌套的arraylists

时间:2017-07-08 13:47:05

标签: android json arraylist gson retrofit

我正在尝试从网址(JSON)=下载数据 https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json

对于我已经使用过改造的网络,这些是我的类,每当我尝试访问嵌套的类或列表时,它都会抛出一个致命的错误。

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类

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

}

我的成分类

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

}

改造课程

构建器

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

堆栈跟踪

 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:无法获得MQSService。

1 个答案:

答案 0 :(得分:1)

好吧,setText不允许整数。

所以而不是

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

使用

text.setText(String.valueof(mRecipies.get(3).getIngredients().size()));
相关问题