2017-07-08 69 views
0

我試圖從URL(JSON)= https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json改造犯規顯示嵌套的ArrayList

爲我用改裝的網絡下載數據,而這些都是我的學生,每當我嘗試訪問嵌套類或列表,它會拋出一個致命的錯誤。

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

那麼,setText不允許整數。

所以不是

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

使用

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

是啊,謝謝,就是這樣,對不起愚蠢的錯誤 –