gson JSON onResponse -列表将不会填充数据

f2uvfpb9  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(180)

我得到了一个工作的应用程序(有点)。它是为了填充屏幕从JSON数据的请求。我以为我有什么到目前为止会工作,但我猜不是。onResponse似乎从来没有被调用,所以数据从来没有被分配到一个变量进行处理,我不知道如何解决这个问题。任何和所有的帮助是非常感谢。
下面是我的相关代码:
我的JSON数据是:

  1. {
  2. "popular":[
  3. {
  4. "name":"Pizza",
  5. "imageURL":"https://static4.depositphotos.com/1016418/315/i/600/depositphotos_3158962-stock-photo-pepperoni-pizza-isolated.jpg",
  6. "rating":"4.8",
  7. "deliveryTime":"45 min",
  8. "deliveryCharges":"Free Delivery",
  9. "price":"15",
  10. "note":"Delicious"
  11. },
  12. {
  13. "name":"Chick-Fil-A",
  14. "imageURL":"https://media.istockphoto.com/photos/chicken-bacon-club-sandwich-picture-id585602032?k=6&m=585602032&s=612x612&w=0&h=DzlMVMOaqC25Zf78NPGzr9nLOf7wANEgv9u_vScu4d8=",
  15. "rating":"5.0",
  16. "deliveryTime":"15 min",
  17. "deliveryCharges":"3",
  18. "price":"7",
  19. "note":"Classic"
  20. },
  21. {
  22. "name":"Mac & Cheese",
  23. "imageURL":"https://media.istockphoto.com/photos/homemade-baked-creamy-macaroni-and-cheese-picture-id483485720?k=6&m=483485720&s=612x612&w=0&h=u2Wb4hR0cIqNAm8q_4j-oFPrtC4hT_YXaq2srA2zmqI=",
  24. "rating":"4.7",
  25. "deliveryTime":"20 min",
  26. "deliveryCharges":"1",
  27. "price":"4",
  28. "note":"Homestyle"
  29. }
  30. ],
  31. "recommended":[
  32. {
  33. "name":"Chicken Tikka Masala",
  34. "imageURL":"https://media.istockphoto.com/photos/chicken-tikka-masala-curry-with-rice-and-naan-bread-picture-id1143530019?k=6&m=1143530019&s=612x612&w=0&h=aOCxTVcwi88NHCX9-xgNu7cmPX9rq5AGtEVYblNTnGc=",
  35. "rating":"4.8",
  36. "deliveryTime":"25 min",
  37. "deliveryCharges":"2",
  38. "price":"14",
  39. "note":"Fan Favorite"
  40. },
  41. {
  42. "name":"Strawberry Milkshake",
  43. "imageURL":"https://thumbs.dreamstime.com/b/strawberry-milkshake-covered-whipped-cream-plastic-glass-isolated-white-background-44432579.jpg",
  44. "rating":"4.5",
  45. "deliveryTime":"10 min",
  46. "deliveryCharges":"0",
  47. "price":"4",
  48. "note":"Chilly"
  49. },
  50. {
  51. "name":"3 Tacos",
  52. "imageURL":"https://previews.123rf.com/images/gdolgikh/gdolgikh1703/gdolgikh170300221/74432760-mexican-tacos-with-beef.jpg",
  53. "rating":"4.6",
  54. "deliveryTime":"17 min",
  55. "deliveryCharges":"1",
  56. "price":"12",
  57. "note":"Hard or Soft Shell"
  58. }
  59. ],
  60. "allmenu":[
  61. {
  62. "name":"Pizza",
  63. "imageURL":"https://static4.depositphotos.com/1016418/315/i/600/depositphotos_3158962-stock-photo-pepperoni-pizza-isolated.jpg",
  64. "rating":"4.8",
  65. "deliveryTime":"45 min",
  66. "deliveryCharges":"Free Delivery",
  67. "price":"15",
  68. "note":"Delicious"
  69. },
  70. {
  71. "name":"Chick-Fil-A",
  72. "imageURL":"https://media.istockphoto.com/photos/chicken-bacon-club-sandwich-picture-id585602032?k=6&m=585602032&s=612x612&w=0&h=DzlMVMOaqC25Zf78NPGzr9nLOf7wANEgv9u_vScu4d8=",
  73. "rating":"5.0",
  74. "deliveryTime":"15 min",
  75. "deliveryCharges":"3",
  76. "price":"7",
  77. "note":"Classic"
  78. },
  79. {
  80. "name":"Mac & Cheese",
  81. "imageURL":"https://media.istockphoto.com/photos/homemade-baked-creamy-macaroni-and-cheese-picture-id483485720?k=6&m=483485720&s=612x612&w=0&h=u2Wb4hR0cIqNAm8q_4j-oFPrtC4hT_YXaq2srA2zmqI=",
  82. "rating":"4.7",
  83. "deliveryTime":"20 min",
  84. "deliveryCharges":"1",
  85. "price":"4",
  86. "note":"Homestyle"
  87. },
  88. {
  89. "name":"Chicken Tikka Masala",
  90. "imageURL":"https://media.istockphoto.com/photos/chicken-tikka-masala-curry-with-rice-and-naan-bread-picture-id1143530019?k=6&m=1143530019&s=612x612&w=0&h=aOCxTVcwi88NHCX9-xgNu7cmPX9rq5AGtEVYblNTnGc=",
  91. "rating":"4.8",
  92. "deliveryTime":"25 min",
  93. "deliveryCharges":"2",
  94. "price":"14",
  95. "note":"Fan Favorite"
  96. },
  97. {
  98. "name":"Strawberry Milkshake",
  99. "imageURL":"https://thumbs.dreamstime.com/b/strawberry-milkshake-covered-whipped-cream-plastic-glass-isolated-white-background-44432579.jpg",
  100. "rating":"4.5",
  101. "deliveryTime":"10 min",
  102. "deliveryCharges":"0",
  103. "price":"4",
  104. "note":"Chilly"
  105. },
  106. {
  107. "name":"3 Tacos",
  108. "imageURL":"https://previews.123rf.com/images/gdolgikh/gdolgikh1703/gdolgikh170300221/74432760-mexican-tacos-with-beef.jpg",
  109. "rating":"4.6",
  110. "deliveryTime":"17 min",
  111. "deliveryCharges":"1",
  112. "price":"12",
  113. "note":"Hard or Soft Shell"
  114. }
  115. ]
  116. }

我有适用于AllmenuFoodDataPopularRecommended的模型。
所有菜单:

  1. package com.example.bearcateats.model;
  2. import javax.annotation.Generated;
  3. import com.google.gson.annotations.Expose;
  4. import com.google.gson.annotations.SerializedName;
  5. @Generated("jsonschema2pojo")
  6. public class Allmenu {
  7. @SerializedName("name")
  8. @Expose
  9. private String name;
  10. @SerializedName("imageURL")
  11. @Expose
  12. private String imageURL;
  13. @SerializedName("rating")
  14. @Expose
  15. private String rating;
  16. @SerializedName("deliveryTime")
  17. @Expose
  18. private String deliveryTime;
  19. @SerializedName("deliveryCharges")
  20. @Expose
  21. private String deliveryCharges;
  22. @SerializedName("price")
  23. @Expose
  24. private String price;
  25. @SerializedName("note")
  26. @Expose
  27. private String note;
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getImageURL() {
  35. return imageURL;
  36. }
  37. public void setImageURL(String imageURL) {
  38. this.imageURL = imageURL;
  39. }
  40. public String getRating() {
  41. return rating;
  42. }
  43. public void setRating(String rating) {
  44. this.rating = rating;
  45. }
  46. public String getDeliveryTime() {
  47. return deliveryTime;
  48. }
  49. public void setDeliveryTime(String deliveryTime) {
  50. this.deliveryTime = deliveryTime;
  51. }
  52. public String getDeliveryCharges() {
  53. return deliveryCharges;
  54. }
  55. public void setDeliveryCharges(String deliveryCharges) {
  56. this.deliveryCharges = deliveryCharges;
  57. }
  58. public String getPrice() {
  59. return price;
  60. }
  61. public void setPrice(String price) {
  62. this.price = price;
  63. }
  64. public String getNote() {
  65. return note;
  66. }
  67. public void setNote(String note) {
  68. this.note = note;
  69. }
  70. }

食品数据:

  1. package com.example.bearcateats.model;
  2. import java.util.List;
  3. import javax.annotation.Generated;
  4. import com.google.gson.annotations.Expose;
  5. import com.google.gson.annotations.SerializedName;
  6. @Generated("jsonschema2pojo")
  7. public class FoodData {
  8. @SerializedName("popular")
  9. @Expose
  10. private List<Popular> popular = null;
  11. @SerializedName("recommended")
  12. @Expose
  13. private List<Recommended> recommended = null;
  14. @SerializedName("allmenu")
  15. @Expose
  16. private List<Allmenu> allmenu = null;
  17. public List<Popular> getPopular() {
  18. return popular;
  19. }
  20. public void setPopular(List<Popular> popular) {
  21. this.popular = popular;
  22. }
  23. public List<Recommended> getRecommended() {
  24. return recommended;
  25. }
  26. public void setRecommended(List<Recommended> recommended) {
  27. this.recommended = recommended;
  28. }
  29. public List<Allmenu> getAllmenu() {
  30. return allmenu;
  31. }
  32. public void setAllmenu(List<Allmenu> allmenu) {
  33. this.allmenu = allmenu;
  34. }
  35. }

人气:

  1. package com.example.bearcateats.model;
  2. import javax.annotation.Generated;
  3. import com.google.gson.annotations.Expose;
  4. import com.google.gson.annotations.SerializedName;
  5. @Generated("jsonschema2pojo")
  6. public class Popular {
  7. @SerializedName("name")
  8. @Expose
  9. private String name;
  10. @SerializedName("imageURL")
  11. @Expose
  12. private String imageURL;
  13. @SerializedName("rating")
  14. @Expose
  15. private String rating;
  16. @SerializedName("deliveryTime")
  17. @Expose
  18. private String deliveryTime;
  19. @SerializedName("deliveryCharges")
  20. @Expose
  21. private String deliveryCharges;
  22. @SerializedName("price")
  23. @Expose
  24. private String price;
  25. @SerializedName("note")
  26. @Expose
  27. private String note;
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getImageURL() {
  35. return imageURL;
  36. }
  37. public void setImageURL(String imageURL) {
  38. this.imageURL = imageURL;
  39. }
  40. public String getRating() {
  41. return rating;
  42. }
  43. public void setRating(String rating) {
  44. this.rating = rating;
  45. }
  46. public String getDeliveryTime() {
  47. return deliveryTime;
  48. }
  49. public void setDeliveryTime(String deliveryTime) {
  50. this.deliveryTime = deliveryTime;
  51. }
  52. public String getDeliveryCharges() {
  53. return deliveryCharges;
  54. }
  55. public void setDeliveryCharges(String deliveryCharges) {
  56. this.deliveryCharges = deliveryCharges;
  57. }
  58. public String getPrice() {
  59. return price;
  60. }
  61. public void setPrice(String price) {
  62. this.price = price;
  63. }
  64. public String getNote() {
  65. return note;
  66. }
  67. public void setNote(String note) {
  68. this.note = note;
  69. }
  70. }

建议:

  1. package com.example.bearcateats.model;
  2. import javax.annotation.Generated;
  3. import com.google.gson.annotations.Expose;
  4. import com.google.gson.annotations.SerializedName;
  5. @Generated("jsonschema2pojo")
  6. public class Recommended {
  7. @SerializedName("name")
  8. @Expose
  9. private String name;
  10. @SerializedName("imageURL")
  11. @Expose
  12. private String imageURL;
  13. @SerializedName("rating")
  14. @Expose
  15. private String rating;
  16. @SerializedName("deliveryTime")
  17. @Expose
  18. private String deliveryTime;
  19. @SerializedName("deliveryCharges")
  20. @Expose
  21. private String deliveryCharges;
  22. @SerializedName("price")
  23. @Expose
  24. private String price;
  25. @SerializedName("note")
  26. @Expose
  27. private String note;
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getImageURL() {
  35. return imageURL;
  36. }
  37. public void setImageURL(String imageURL) {
  38. this.imageURL = imageURL;
  39. }
  40. public String getRating() {
  41. return rating;
  42. }
  43. public void setRating(String rating) {
  44. this.rating = rating;
  45. }
  46. public String getDeliveryTime() {
  47. return deliveryTime;
  48. }
  49. public void setDeliveryTime(String deliveryTime) {
  50. this.deliveryTime = deliveryTime;
  51. }
  52. public String getDeliveryCharges() {
  53. return deliveryCharges;
  54. }
  55. public void setDeliveryCharges(String deliveryCharges) {
  56. this.deliveryCharges = deliveryCharges;
  57. }
  58. public String getPrice() {
  59. return price;
  60. }
  61. public void setPrice(String price) {
  62. this.price = price;
  63. }
  64. public String getNote() {
  65. return note;
  66. }
  67. public void setNote(String note) {
  68. this.note = note;
  69. }
  70. }

主要:

  1. package com.example.bearcateats;
  2. import android.os.Bundle;
  3. import android.widget.Toast;
  4. import retrofit2.Call;
  5. import retrofit2.Callback;
  6. import retrofit2.Response;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import androidx.recyclerview.widget.LinearLayoutManager;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import com.example.bearcateats.adapters.PopularAdapter;
  11. import com.example.bearcateats.adapters.RecommendedAdapter;
  12. import com.example.bearcateats.model.Allmenu;
  13. import com.example.bearcateats.model.FoodData;
  14. import com.example.bearcateats.model.Popular;
  15. import com.example.bearcateats.model.Recommended;
  16. import com.example.bearcateats.retrofit.ApiInterface;
  17. import com.example.bearcateats.retrofit.RetrofitClient;
  18. import com.example.bearcateats.adapters.AllMenuAdapter;
  19. import java.util.List;
  20. public class MainActivity extends AppCompatActivity {
  21. ApiInterface apiInterface;
  22. RecyclerView popularRecyclerView, recommendedRecyclerView, allMenuRecyclerView;
  23. PopularAdapter popularAdapter;
  24. RecommendedAdapter recommendedAdapter;
  25. AllMenuAdapter allMenuAdapter;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. apiInterface = RetrofitClient.getRetrofitInstance().create(ApiInterface.class);
  31. Call<List<FoodData>> call = apiInterface.getAllData();
  32. call.enqueue(new Callback<List<FoodData>>() {
  33. @Override
  34. public void onResponse(Call<List<FoodData>> call, Response<List<FoodData>> response) {
  35. System.out.println("TEST!!!!!!!!!!!!!!");
  36. List<FoodData> MenuList;
  37. MenuList = response.body();
  38. if(MenuList == null) {
  39. System.out.println("MenuList!!!!!!!!!!!!!!");
  40. }
  41. List <FoodData> test1 = (List<FoodData>) MenuList.get(0);
  42. List<Popular> popular = ((FoodData) test1).getPopular();
  43. getPopularData(popular);
  44. System.out.println("GET POPULAR RAN!!!!!!!!!!!!!!");
  45. getRecommendedData(MenuList.get(0).getRecommended());
  46. }
  47. @Override
  48. public void onFailure(Call<List<FoodData>> call, Throwable t) {
  49. Toast.makeText(MainActivity.this, "Server is not responding.", Toast.LENGTH_SHORT).show();
  50. }
  51. });
  52. }
  53. private void getPopularData(List<Popular> popularList){
  54. popularRecyclerView = findViewById(R.id.popular_recycler);
  55. popularAdapter = new PopularAdapter(this, popularList);
  56. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  57. popularRecyclerView.setLayoutManager(layoutManager);
  58. popularRecyclerView.setAdapter(popularAdapter);
  59. }
  60. private void getRecommendedData(List<Recommended> recommendedList){
  61. recommendedRecyclerView = findViewById(R.id.recommended_recycler);
  62. recommendedAdapter = new RecommendedAdapter(this, recommendedList);
  63. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  64. recommendedRecyclerView.setLayoutManager(layoutManager);
  65. recommendedRecyclerView.setAdapter(recommendedAdapter);
  66. }
  67. private void getAllMenu(List<Allmenu> allmenuList){
  68. allMenuRecyclerView = findViewById(R.id.all_menu_recycler);
  69. allMenuAdapter = new AllMenuAdapter(this, allmenuList);
  70. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
  71. allMenuRecyclerView.setLayoutManager(layoutManager);
  72. allMenuRecyclerView.setAdapter(allMenuAdapter);
  73. allMenuAdapter.notifyDataSetChanged();
  74. }
  75. }

食物详情

  1. {
  2. // now we will get data from intent and set to UI
  3. ImageView imageView;
  4. TextView itemName, itemPrice, itemRating;
  5. RatingBar ratingBar;
  6. String name, price, rating, imageUrl;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_food_details);
  11. Intent intent = getIntent();
  12. name = intent.getStringExtra("name");
  13. price = intent.getStringExtra("price");
  14. rating = intent.getStringExtra("rating");
  15. imageUrl = intent.getStringExtra("image");
  16. imageView = findViewById(R.id.imageView5);
  17. itemName = findViewById(R.id.name);
  18. itemPrice = findViewById(R.id.price);
  19. itemRating = findViewById(R.id.rating);
  20. ratingBar = findViewById(R.id.ratingBar);
  21. Glide.with(getApplicationContext()).load(imageUrl).into(imageView);
  22. itemName.setText(name);
  23. itemPrice.setText("$ "+price);
  24. itemRating.setText(rating);
  25. ratingBar.setRating(Float.parseFloat(rating));
  26. }
  27. }
ef1yzkbh

ef1yzkbh1#

有几个愚蠢的错误。我已经修复了你所有的错误。你的JSONResponse数据类没有任何错误。正确的responseonResponse()回调中被接收。问题存在于你代码的其他部分。只要一步一步地遵循。

步骤-1

在解析api调用中的response数据时,替换以下代码,如下所示:

  1. call.enqueue(new Callback<List<FoodData>>() {
  2. @Override
  3. public void onResponse(Call<List<FoodData>> call, Response<List<FoodData>> response) {
  4. if (response.isSuccessful()){
  5. if (response.body() != null){
  6. List<Popular> popular = response.body().get(0).getPopular();
  7. getPopularData(popular);
  8. getRecommendedData(response.body().get(0).getRecommended());
  9. }
  10. }
  11. }
  12. @Override
  13. public void onFailure(Call<List<FoodData>> call, Throwable t) {
  14. Toast.makeText(MainActivity.this, "Server is not responding.", Toast.LENGTH_SHORT).show();
  15. }
  16. });

步骤2

PopularAdapter类中替换getItemCount()方法,如下所示:

  1. @Override
  2. public int getItemCount() {
  3. //return 0; // Returning 0 will show nothing in RecyclerView.
  4. return popularList.size();
  5. }

步骤3:

RecommendedAdapter类中,您将错误的xml用于ViewHolder。请按如下所示进行替换:

  1. @NonNull
  2. @Override
  3. public RecommendedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  4. //View view = LayoutInflater.from(context).inflate(R.layout.popular_recycler_items, parent, false);
  5. View view = LayoutInflater.from(context).inflate(R.layout.recommended_recyvler_items, parent, false);
  6. return new RecommendedViewHolder(view);
  7. }

并且也像下面这样替换getItemCount()的方法:

  1. @Override
  2. public int getItemCount() {
  3. return recommendedList.size();
  4. }

更新日期:-

为什么在api调用的onSuccess()内调用相同的api?我已经更新了您的onCreate()方法,如下所示:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. apiInterface = RetrofitClient.getRetrofitInstance().create(ApiInterface.class);
  6. Call<List<FoodData>> call = apiInterface.getAllData();
  7. call.enqueue(new Callback<List<FoodData>>() {
  8. @Override
  9. public void onResponse(Call<List<FoodData>> call, Response<List<FoodData>> response) {
  10. if (response.isSuccessful()) {
  11. if (response.body() != null) {
  12. getPopularData(response.body().get(0).getPopular());
  13. getRecommendedData(response.body().get(0).getRecommended());
  14. getAllMenu(response.body().get(0).getAllmenu());
  15. }
  16. }
  17. /*
  18. call.clone().enqueue(new Callback<List<FoodData>>() {
  19. @Override
  20. public void onResponse(Call<List<FoodData>> call, Response<List<FoodData>> response) {
  21. if (response.isSuccessful()) {
  22. if (response.body() != null) {
  23. response.code();
  24. getPopularData(response.body().get(0).getPopular());
  25. getRecommendedData(response.body().get(0).getRecommended());
  26. getAllMenu(response.body().get(0).getAllmenu());
  27. }
  28. }
  29. }
  30. @Override
  31. public void onFailure(Call<List<FoodData>> call, Throwable t) {
  32. response.code();
  33. Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
  34. }
  35. });*/
  36. }
  37. @Override
  38. public void onFailure(Call<List<FoodData>> call, Throwable t) {
  39. Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
  40. }
  41. });
  42. }
展开查看全部
ix0qys7i

ix0qys7i2#

我看到https://francescainfranca.github.io/menuJSON/index.json中的JSON是单个FoodData JSON对象,而不是JSON数组。
这会在运行应用程序时导致以下异常。因此,请更改JSON结构或使应用程序直接使用FoodData,而不是List .

此外,吐司消息(服务器没有响应)也显示在您已添加“失败响应”的运行上。
希望这对你有帮助谢谢。

相关问题