2

我在理解如何使用 amazon mws java 库时遇到问题。我能够实现它,我可以调用 ListMatchingProducts 并将一些 xml 打印到控制台中。但是我如何从中获取部分信息,例如产品标题?我刚刚开始编程,可能我只是不了解这里的 api 使用基础知识。请帮帮我。这就是我调用搜索方法的方式:

   searchF.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            AmazonSearch.amazonSearch(searchF.getText());               
        }
    });

这是我想使用的亚马逊 api 类的摘录:

         invokeListMatchingProducts(service, request);

    }

    public static ListMatchingProductsResponse response;

    public static void invokeListMatchingProducts(MarketplaceWebServiceProducts service, 
            ListMatchingProductsRequest request) {
        try {

            response = service.listMatchingProducts(request);


            System.out.println ("ListMatchingProducts Action Response");
            System.out.println ("=============================================================================");
            System.out.println ();

            System.out.println("    ListMatchingProductsResponse");
            System.out.println();
            if (response.isSetListMatchingProductsResult()) {
                System.out.println("        ListMatchingProductsResult");
                System.out.println();
                ListMatchingProductsResult  listMatchingProductsResult = response.getListMatchingProductsResult();
                if (listMatchingProductsResult.isSetProducts()) {
                    System.out.println("            Products");
                    System.out.println();
                    ProductList  products = listMatchingProductsResult.getProducts();
                    java.util.List<Product> productList = products.getProduct();
                    for (Product product : productList) {
                        System.out.println("                Product");
                        System.out.println();
                        if (product.isSetIdentifiers()) {
                            System.out.println("                    Identifiers");
                            System.out.println();
                            IdentifierType  identifiers = product.getIdentifiers();
                            if (identifiers.isSetMarketplaceASIN()) {
                                System.out.println("                        MarketplaceASIN");
                                System.out.println();
                                ASINIdentifier  marketplaceASIN = identifiers.getMarketplaceASIN();
                                if (marketplaceASIN.isSetMarketplaceId()) {
                                    System.out.println("                            MarketplaceId");
                                    System.out.println();
                                    System.out.println("                                " + marketplaceASIN.getMarketplaceId());
                                    System.out.println();
                                }
                                if (marketplaceASIN.isSetASIN()) {
                                    System.out.println("                            ASIN");
                                    System.out.println();
                                    System.out.println("                                " + marketplaceASIN.getASIN());
                                    System.out.println();
                                }
                            } 
                            if (identifiers.isSetSKUIdentifier()) {
                                System.out.println("                        SKUIdentifier");
                                System.out.println();
                                SellerSKUIdentifier  SKUIdentifier = identifiers.getSKUIdentifier();
                                if (SKUIdentifier.isSetMarketplaceId()) {
                                    System.out.println("                            MarketplaceId");
                                    System.out.println();
                                    System.out.println("                                " + SKUIdentifier.getMarketplaceId());
                                    System.out.println();
                                }
                                if (SKUIdentifier.isSetSellerId()) {
                                    System.out.println("                            SellerId");
                                    System.out.println();
                                    System.out.println("                                " + SKUIdentifier.getSellerId());
                                    System.out.println();
                                }
                                if (SKUIdentifier.isSetSellerSKU()) {
                                    System.out.println("                            SellerSKU");
                                    System.out.println();
                                    System.out.println("                                " + SKUIdentifier.getSellerSKU());
                                    System.out.println();
                                }
                            } 
                        } 
                        if (product.isSetAttributeSets()) {
                            System.out.println("                    Attributes");
                            AttributeSetList attributeSetList = product.getAttributeSets();
                            for (Object obj : attributeSetList.getAny()) {
                                Node attribute = (Node) obj;
                                System.out.println(ProductsUtil.formatXml(attribute));
                            }
                            System.out.println();
                        }
                       ...
                    }
                } 
            } 
            if (response.isSetResponseMetadata()) {
                System.out.println("        ResponseMetadata");
                System.out.println();
                ResponseMetadata  responseMetadata = response.getResponseMetadata();
                if (responseMetadata.isSetRequestId()) {
                    System.out.println("            RequestId");
                    System.out.println();
                    System.out.println("                " + responseMetadata.getRequestId());
                    System.out.println();
                }
            } 
            System.out.println();
            System.out.println(response.getResponseHeaderMetadata());
            System.out.println();


        } catch (MarketplaceWebServiceProductsException ex) {

            System.out.println("Caught Exception: " + ex.getMessage());
            System.out.println("Response Status Code: " + ex.getStatusCode());
            System.out.println("Error Code: " + ex.getErrorCode());
            System.out.println("Error Type: " + ex.getErrorType());
            System.out.println("Request ID: " + ex.getRequestId());
            System.out.println("XML: " + ex.getXML());
            System.out.print("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
        }
    }

}
4

1 回答 1

1

让它自己工作。我只需要访问public static ListMatchingProductsResponse response;并将其转换为 xml。其实没那么难。

于 2013-01-03T19:29:12.563 回答