作为大学作业,我需要创建一个应用程序,从荷兰著名的在线商店API中检索产品数据。我需要将每个产品的标题、摘要、价格和图像URL存储到新的Product对象中。这些产品存储在ArrayList中,然后返回ArrayList。
产品数组中的每个产品都有一个名为“images”的嵌套数组,其中包含6个产品图像。这些图像需要存储到我的Product对象的HashMap属性中,以图像大小作为键,URL作为值。但是,我似乎无法做到这一点。
使用查询“pokemon”的JSON数据:https://api.bol.com/catalog/v4/search/?apikey=25C4742A92BF468EB2BD888FC8FBFF40&format=json&q=pokemon 产品类:
解析JSON方法:
当我测试我的应用程序时,似乎它已将最后一个产品的图像URL存储到每个产品的HashMap中:
产品数组中的每个产品都有一个名为“images”的嵌套数组,其中包含6个产品图像。这些图像需要存储到我的Product对象的HashMap属性中,以图像大小作为键,URL作为值。但是,我似乎无法做到这一点。
使用查询“pokemon”的JSON数据:https://api.bol.com/catalog/v4/search/?apikey=25C4742A92BF468EB2BD888FC8FBFF40&format=json&q=pokemon 产品类:
package com.example.bolcombrowser.domain;
import java.util.Map;
public class Product {
// Attributes
private String mTitle;
private String mSummary;
private double mPrice;
private Map < String, String > mImageUrls;
// Constructor
public Product(String mTitle, String mSummary, double mPrice, Map < String, String > mImageUrls) {
this.mTitle = mTitle;
this.mSummary = mSummary;
this.mPrice = mPrice;
this.mImageUrls = mImageUrls;
}
// Getters and Setters
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmSummary() {
return mSummary;
}
public void setmSummary(String mSummary) {
this.mSummary = mSummary;
}
public double getmPrice() {
return mPrice;
}
public void setmPrice(double mPrice) {
this.mPrice = mPrice;
}
public Map < String, String > getImageUrls() {
return mImageUrls;
}
public void setImageUrls(Map < String, String > imageUrls) {
this.mImageUrls = imageUrls;
}
}
解析JSON方法:
public static ArrayList < Product > parseJson(String productJsonStr) throws JSONException {
/* JSON array names. */
final String BOL_PRODUCTS = "products";
final String BOL_IMAGES = "images";
final String BOL_OFFERS = "offers";
/* JSON key names. */
final String BOL_TITLE = "title";
final String BOL_SUMMARY = "summary";
final String BOL_OFFERDATA = "offerData";
final String BOL_PRICE = "price";
final String BOL_KEY = "key";
final String BOL_URL = "url";
/* Variables to store product data into, and is then used to create new Product objects. */
String title;
String summary;
double price;
Map < String, String > imageUrls = new HashMap < > ();
/* ArrayList to store products into. */
ArrayList < Product > productList = new ArrayList < > ();
JSONObject productsJson = new JSONObject(productJsonStr);
JSONArray productsArray = productsJson.getJSONArray(BOL_PRODUCTS);
for (int i = 0; i < productsArray.length(); i++) {
JSONObject product = productsArray.getJSONObject(i);
/* Retrieve the title and summary of each product. */
title = product.getString(BOL_TITLE);
summary = product.getString(BOL_SUMMARY);
JSONArray imagesArray = product.getJSONArray(BOL_IMAGES);
for (int j = 0; j < imagesArray.length(); j++) {
JSONObject image = imagesArray.getJSONObject(j);
/* Retrieve each product's image sizes and URLs and store them into a HashMap. */
String imageSize = image.getString(BOL_KEY);
String imageUrl = image.getString(BOL_URL);
imageUrls.put(imageSize, imageUrl);
}
JSONObject offerData = product.getJSONObject(BOL_OFFERDATA);
JSONArray offers = offerData.getJSONArray(BOL_OFFERS);
JSONObject offer = offers.getJSONObject(0);
price = offer.getDouble(BOL_PRICE);
productList.add(new Product(title, summary, price, imageUrls));
}
return productList;
}
onPostExecute方法:
@Override
protected void onPostExecute(String productData) {
if (productData != null) {
ArrayList < Product > productList;
try {
productList = JsonUtils.parseJson(productData);
for (Product product: productList) {
String title = product.getmTitle();
String summary = product.getmSummary();
double price = product.getmPrice();
String hashMap = product.getImageUrls().toString();
mTextViewOutput.append(title + "\n\n" + summary + "\n\n" + price + "\n\n" +
hashMap + "\n\n\n\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
当我测试我的应用程序时,似乎它已将最后一个产品的图像URL存储到每个产品的HashMap中:
我已经盯着代码看了几个小时,但似乎找不出它为什么会这样。我可能犯了一个非常愚蠢的错误,但我似乎无法确定它到底是什么。