pez
pez

Reputation: 4235

Amazon AWS Java - list orders throws exception

I'm trying to get orders from Amazon MWS, and I've started by simply copying ListOrdersSample.java in the client library samples. Below is the code, and the StringIndexOutOfBounds exception it throws. I'm not sure why this is happening. Amazon's documentation is very sparse and searching online yields very little results.

Can anyone help? Thank you in advance!

public class GetOrderList 
{
    private static final String mAccessKeyID = "accessKeyID";
    private static final String mSecretAccessKey = "secretAccessKey";
    private static final String mMerchantID = "merchantID";
    private static final String mMarketplaceID = "marketplaceID";

     /**
     * Call the service, log response and exceptions.
     */
    public static ListOrdersResponse invokeListOrders(
            MarketplaceWebServiceOrders client, 
            ListOrdersRequest request) 
    {
        try 
        {
            // Call the service.
            // Line 44
            ListOrdersResponse response = client.listOrders(request);
            ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();

            // We recommend logging every the request id and timestamp of every call.
            System.out.println("Response:");
            System.out.println("RequestId: "+rhmd.getRequestId());
            System.out.println("Timestamp: "+rhmd.getTimestamp());
            String responseXml = response.toXML();
            System.out.println(responseXml);
            return response;
        } 
        catch (MarketplaceWebServiceOrdersException ex) 
        {
            // Exception properties are important for diagnostics.
            System.out.println("Service Exception:");
            ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();

            if(rhmd != null) 
            {
                System.out.println("RequestId: "+rhmd.getRequestId());
                System.out.println("Timestamp: "+rhmd.getTimestamp());
            }

            System.out.println("Message: "+ex.getMessage());
            System.out.println("StatusCode: "+ex.getStatusCode());
            System.out.println("ErrorCode: "+ex.getErrorCode());
            System.out.println("ErrorType: "+ex.getErrorType());
            throw ex;
        }
    }

    /**
     *  Command line entry point.
     */
    public static void main(String[] args) 
    {
        // Get a client connection.
        // Make sure you've set the variables in MarketplaceWebServiceOrdersSampleConfig.
        MarketplaceWebServiceOrdersClient client = MarketplaceWebServiceOrdersSampleConfig.getClient();

        // Create a request.
        ListOrdersRequest request = new ListOrdersRequest();

        request.setSellerId(mMerchantID);

        // Not sure if this is correct
        request.setMWSAuthToken(mAccessKeyID);

        XMLGregorianCalendar createdAfter = MwsUtl.getDTF().newXMLGregorianCalendar();
        request.setCreatedAfter(createdAfter);

        XMLGregorianCalendar createdBefore = MwsUtl.getDTF().newXMLGregorianCalendar();
        request.setCreatedBefore(createdBefore);

        XMLGregorianCalendar lastUpdatedAfter = MwsUtl.getDTF().newXMLGregorianCalendar();
        request.setLastUpdatedAfter(lastUpdatedAfter);

        XMLGregorianCalendar lastUpdatedBefore = MwsUtl.getDTF().newXMLGregorianCalendar();
        request.setLastUpdatedBefore(lastUpdatedBefore);

        List<String> orderStatus = new ArrayList<String>();
        request.setOrderStatus(orderStatus);

        List<String> marketplaceId = new ArrayList<String>();
        request.setMarketplaceId(marketplaceId);

        List<String> fulfillmentChannel = new ArrayList<String>();
        request.setFulfillmentChannel(fulfillmentChannel);

        List<String> paymentMethod = new ArrayList<String>();
        request.setPaymentMethod(paymentMethod);

        String buyerEmail = "example";
        request.setBuyerEmail(buyerEmail);

        String sellerOrderId = "example";
        request.setSellerOrderId(sellerOrderId);

        Integer maxResultsPerPage = 100;
        request.setMaxResultsPerPage(maxResultsPerPage);

        List<String> tfmShipmentStatus = new ArrayList<String>();
        request.setTFMShipmentStatus(tfmShipmentStatus);

        // Make the call.
        // Line 129
        invokeListOrders(client, request);
    }
}


Service Exception:
Message: null
StatusCode: 0
ErrorCode: null
Exception in thread "main" com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersException
ErrorType: null
at com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersClient$RequestType.wrapException(MarketplaceWebServiceOrdersClient.java:143)
at com.amazonservices.mws.client.MwsConnection.call(MwsConnection.java:429)
at com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersClient.listOrders(MarketplaceWebServiceOrdersClient.java:87)
at test.GetOrderList.invokeListOrders(GetOrderList.java:44)
at test.GetOrderList.main(GetOrderList.java:129)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1955)
at com.amazonservices.mws.client.MwsConnection$ServiceEndpoint.<init>(MwsConnection.java:102)
at com.amazonservices.mws.client.MwsConnection.getServiceEndpoint(MwsConnection.java:398)
at com.amazonservices.mws.client.MwsConnection.newCall(MwsConnection.java:687)
at com.amazonservices.mws.client.MwsConnection.call(MwsConnection.java:420)
... 3 more

Upvotes: 1

Views: 1208

Answers (2)

Dave Patrick
Dave Patrick

Reputation: 298

I received this exception because my ServiceURL wasn't set right.

To fix, I set the ServiceURL to https://mws.amazonservices.com

Upvotes: 1

Hazzit
Hazzit

Reputation: 6882

Did you do number 3 on the Quick Start list for the Java MWS library? It seems the service endpoint is not configured.

Upvotes: 0

Related Questions