Chromos
Chromos

Reputation: 1921

Java using Amazon mws?

I have problems understanding how to use the amazon mws java library. I was able to implement it and I can call ListMatchingProducts and it prints me some xml into the console. But how do I get just parts from it, like product title for example? I just getting into programming and probably I just dont understand some basics of api usage here. please help me out. This is how I call the search method:

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

And this is an excerpt from the amazon api class I want to use:

         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());
        }
    }

}

Upvotes: 2

Views: 1762

Answers (1)

Chromos
Chromos

Reputation: 1921

got it working myself. I just needed to access public static ListMatchingProductsResponse response; and convert it to xml. not that difficult actually.

Upvotes: 1

Related Questions