我解析了幾個Json鏈接,並嘗試將所有輸出添加到一個List
。但是,該列表總是被改爲覆蓋,只包括一個鏈接的結果:將多個HTTP請求的結果彙總到單個列表
public class GetShopifyJsonData extends GetRawData {
private String LOG_TAG = GetShopifyJsonData.class.getSimpleName();
private List<Product> mProduct;
private Uri mDestination;
public GetShopifyJsonData(int page) {
super(null);
createUri(page);
mProduct = new ArrayList<Product>();
}
public void execute(){
super.setRawUrl(mDestination.toString());
DownloadShopifyData downloadShopifyData = new DownloadShopifyData();
Log.v(LOG_TAG, "Built URI = " + mDestination.toString());
downloadShopifyData.execute(mDestination.toString());
}
public boolean createUri(int page) {
final String SHOPIFY_BASE_URL = "";
final String SHOPIFY_PAGE_PARAM = "page";
mDestination = Uri.parse(SHOPIFY_BASE_URL).buildUpon()
.appendQueryParameter(SHOPIFY_PAGE_PARAM, String.valueOf(page)).build();
return mDestination != null;
}
public void processResults() {
if(getDownloadStatus() != DownloadStatus.OK){
Log.e(LOG_TAG, "Error Downloading Raw Data");
return;
}
final String SH_PRODUCTS = "products";
final String SH_TYPE = "product_type";
final String SH_VARIANTS = "variants";
final String SH_TITLE = "title";
final String SH_PRICE = "price";
final String SH_GRAMS = "grams";
try {
JSONObject jsonData = new JSONObject(getData());
JSONArray productsArray = jsonData.getJSONArray(SH_PRODUCTS);
for (int i=0; i<productsArray.length(); i++) {
JSONObject jsonProduct = productsArray.getJSONObject(i);
String productType =jsonProduct.getString(SH_TYPE);
String title = jsonProduct.getString(SH_TITLE);
JSONArray variantsArray = jsonProduct.getJSONArray(SH_VARIANTS);
JSONObject variantProduct = variantsArray.getJSONObject(0);
String variantTitle = variantProduct.getString(SH_TITLE);
double price = variantProduct.getDouble(SH_PRICE);
int grams = variantProduct.getInt(SH_GRAMS);
if (productType.equals("Keyboard") || productType.equals("Computer")) {
Product productObject = new Product(title, price, grams, productType, variantTitle);
this.mProduct.add(productObject);
}
}
for(Product singleProduct : mProduct){
Log.v(LOG_TAG, singleProduct.toString());
Log.v(LOG_TAG, String.valueOf(mProduct.size()));
}
} catch (JSONException jsone) {
jsone.printStackTrace();
Log.e(LOG_TAG, "Error Processing JSON data");
}
}
}
而且從MainActivity
電話:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i=1; i<6; i++) {
GetShopifyJsonData jsonData = new GetShopifyJsonData(i);
jsonData.execute();
}
}
什麼我需要改變,以獲得產品中添加彼此在一個單一的列表?
你認爲他們在哪裏被「覆蓋」? – shmosel
@shmosel'Log.v(LOG_TAG,String.valueOf(mProduct.size()));' –