【问题标题】:Retrofit Doesnt show nested arraylists改造不显示嵌套的数组列表
【发布时间】:2017-12-12 16:56:27
【问题描述】:

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

对于我使用改造的网络,这些是我的类,每当我尝试访问嵌套类或列表时,它都会引发致命错误。

主活动

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

        }
    });

我的食谱类

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 失败。

【问题讨论】:

    标签: android json arraylist gson retrofit


    【解决方案1】:

    好吧,setText 不允许使用整数。

    所以不是

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

    使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2022-01-17
      • 2022-06-15
      • 2017-11-25
      • 2016-08-17
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多