Console Add Item –Java使用eBay API SDK刊登商品 详解

准备工做:java

1. 运行Eclipse (或其余Java IDE)

apache

2.建立一个ConsoleAddItem工程(项目)api

   选JDK 1.五、1.六、1.8等版本,已测试1.六、1.8版本。
服务器

 

三、下载JAVA版本的eBay Trading SDKapp

       地址:https://go.developer.ebay.com/javasdk

框架

四、建立一个eBay用户令牌(eBay user token) 备用。

eclipse

五、将下列库添加到项目构建路径(属性->Java Build Path->Libraries):函数

      1) SDK libraries: {JavaSDK}lib目录下ebaysdkcore.jar, ebaycalls.jar, helper.jar工具

      2) JAXWS-RI和其余libraries: {JavaSDK}libexternalLib目录下除了optional文件夹的全部jar测试

      3) JRE System Library

    说明:

        ① eBay SDK library是SDK的主要组成部分。它封装了eBay API调用并为您隐藏了底层的通讯细节。

        ② JAXWS-RI和其余库是第三方库。应用程序在编译和运行时会依赖这些库。





六、在src新建一个名为ApplicationAddItem.java的主程序类,包本身设置。



七、在文件夹src/com/resources下新建一个名为log4j.properties的配置文件(见清单一)。有了这个文件,SDK就会使用log4j做为基础日志框架,它会在控制台输出全部在log4j.properties中定义的日志项。一旦出现错误,日志对于排错会特别有用。

请肯定src/main/resources文件夹在eclipse项目的编译路径中。

eBay SDK使用Java Simple Logging Facade(SLF4J),它是一个能够使用多种不一样基础日志框架(好比log4j, java.util.logging)的简单门面。默认状况下eBay SDK使用log4j做为基础日志框架,若是您愿意的话,您能够选择其余的日志框架,例如java.util.logging。详情请参考您SDK安装根目录的doc/readme.htm文件中[关于日志的注意事项]部分。

清单一  日志配置

# ***** Set root logger level to debug and its appender to stdout.
log4j.rootLogger=DEBUG, stdout 

# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# ***** Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d%n%m%n%n

 

八、为了调用eBay API,首先你须要初始化ApiContext对象。ApiContext对象包含了全部调用eBay API所需的配置信息。

在主程序类中,新建一个名为getApiContext的方法(见清单二)。在这个方法中,咱们经过获取用户在控制台中输入的用户令牌和服务器地址来初始化ApiContext对象。

清单二  初始化ApiContext对象

/**
     * 输入eBay SDK ApiContext 用户数据
     * 
     * @return ApiContext 对象
     * @throws IOException
     */
    private static ApiContext getApiContext() throws IOException {

        String input;
        ApiContext apiContext = new ApiContext();

        // 设置API Token 访问 eBay API服务器
        ApiCredential cred = apiContext.getApiCredential();
        input = ConsoleUtil.readString("输入您的eBay身份验证Token: ");
        cred.seteBayToken(input);

        // 设置API服务器URL
        input = ConsoleUtil.readString("输入 eBay SOAP 服务器URL (e.g., https://api.ebay.com/wsapi): ");
        apiContext.setApiServerUrl(input);

// 设置EPS图片服务器URL
        apiContext.setEpsServerUrl("https://api.sandbox.ebay.com/ws/api.dll");
return apiContext; }


九、为了在eBay站点上登陆一个商品,首先您须要新建ItemType类的一个实例。

在主程序类中,新建一个buildItem的方法(见清单三)。在这个方法中,咱们经过具体商品的标题、描述、类别、支付方法和货运细节等信息来构建一个item。关于如何构造item的详细信息,请参考eBay Trading API documentati.

清单三  构建ItemType类

/**
     *  构建ItemType类
     * @return ItemType  对象
     * @throws IOException
     */
    private static  ItemType builItem() throws IOException {
        String input;
        ItemType item = new ItemType();
        
        // 项目标题
        item.setTitle(ConsoleUtil.readString("标题: "));

// 画廊图片
        String[] paths = {"http://www.dilianidc.com/templets/twang/images/tw_11.jpg","http://www.dilianidc.com/templets/twang/images/tw_20.jpg"};
        PictureDetailsType pic = new PictureDetailsType();
        pic.setPictureURL(paths);
        //指定画廊图片
        pic.setGalleryType(GalleryTypeCodeType.GALLERY);
        item.setPictureDetails(pic);
    
        // 项目SKU
        item.setSKU(ConsoleUtil.readString("SKU: "));
// 项目描述 item.setDescription(ConsoleUtil.readString("描述:")); // 类型 item.setListingType(ListingTypeCodeType.CHINESE); // 货币 item.setCurrency(CurrencyCodeType.USD); input = ConsoleUtil.readString("价格: "); AmountType amount = new AmountType(); amount.setValue(Double.valueOf(input)); item.setStartPrice(amount); //上市时间 item.setListingDuration(ListingDurationCodeType.DAYS_3.value()); // 项目地点和国家 item.setLocation(ConsoleUtil.readString("地点位置")); item.setCountry(CountryCodeType.US); // 分类 CategoryType cat = new CategoryType(); cat.setCategoryID(ConsoleUtil.readString("eBay类别 (e.g., 30022): ")); item.setPrimaryCategory(cat); // 库存数量 item.setQuantity(new Integer(1)); // 付款方式 item.setPaymentMethods(new BuyerPaymentMethodCodeType[]{BuyerPaymentMethodCodeType.PAY_PAL}); //设置 setPayPal 付款方式 item.setPayPalEmailAddress("888@yirose.com"); // item 状态, 全新(1000) item.setConditionID(1000); // 须要处理时间 item. setDispatchTimeMax(Integer.valueOf(1)); // 运输细节 item.setShippingDetails(buildShippingDetails()); // 退货政策 ReturnPolicyType returnPolicy = new ReturnPolicyType(); returnPolicy.setReturnsAcceptedOption("ReturnsAccepted"); item.setReturnPolicy(returnPolicy); return item; }

 

十、在主程序类中,建立一个名为buildShippingDetails的工具方法(见清单四)。该方法用于构造货运细节,buildItem方法会调用这个方法。

清单四  构建buildShippingDetails类

    /**
     * 构建产品运输细节
     * @return ShippingDetailsType 对象
     */
    private static ShippingDetailsType buildShippingDetails() {
        
        // 运输细节
        ShippingDetailsType sd =new ShippingDetailsType();
        
        sd.setApplyShippingDiscount(new Boolean(true));
        AmountType amount =new AmountType();
        amount.setValue(2.8);
        sd.setPaymentInstructions("eBay Java SDK 测试指令.");
        
        // 选择航运类型和航运服务
        sd.setShippingType(ShippingTypeCodeType.FLAT);
        ShippingServiceOptionsType shippingOptions  = new ShippingServiceOptionsType();
        shippingOptions.setShippingService(ShippingServiceCodeType.SHIPPING_METHOD_STANDARD.value());
        
        //amount = new AmountType();
        amount.setValue(2.0);
        shippingOptions.setShippingServiceAdditionalCost(amount);
        
        //amount  = new AmountType();
        amount.setValue(10);
        shippingOptions.setShippingServiceCost(amount);
        shippingOptions.setShippingServicePriority(new Integer(1));
        
        //amount  = new AmountType();
        amount.setValue(1);
        shippingOptions.setShippingInsuranceCost(amount);
        
        sd.setShippingServiceOptions(new ShippingServiceOptionsType[]{shippingOptions});
                
        return sd;
    }

 

十一、如今万事俱备,咱们将各个部分在main方法中组合起来吧(见清单五)

 

清单五  main方法组合

 

public static void main(String[] args) {
        try {

            System.out.println(" ");
            System.out.println("+++++++++++++++++++++++++++++++++++++++ ");
            System.out.println("+ 欢迎使用eBay API  JAVA实例 + ");
            System.out.println("+  - ConsoleAddItem   + ");
            System.out.println("+++++++++++++++++++++++++++++++++++++++ ");
            System.out.println(" ");

            // [Step 1] eBay ApiContext 对象初始化
            System.out.println("==== [1] 账户信息  ====");
            ApiContext apiContext = getApiContext();

            // [Step 2] 建立新的 item 对象
            System.out.println("==== [2] Item信息  ====");
            ItemType item = builItem();

            // [Step 3] 建立调用对象并执行调用
            System.out.println("==== [3] 执行API调用  ====");
            System.out.println("开始调用eBay API,请稍候…  ");
            AddItemCall api = new AddItemCall(apiContext);
            api.setItem(item);
            FeesType fees = api.addItem();
            System.out.println("调用eBay API结束,显示调用结果…");
            System.out.println();

            // [Step 4] 显示结果
            System.out.println("Listing已成功发布!");

            double listingFee = eBayUtil.findFeeByName(fees.getFee(), "ListingFee").getFee().getValue();
            System.out.println("Listing 费用是:" + new Double(listingFee).toString());
            System.out.println("Listed Item ID:" + item.getItemID());

        } catch (Exception e) {
            System.out.println("错误!没有发布Listing");
            e.printStackTrace();
        }
    }

 

ReviseItem 代码  修改Step 3

            // [Step 3] 建立调用对象并执行调用
            System.out.println("==== [3] 执行API调用  ====");
            System.out.println("开始调用eBay API,请稍候…  ");

            ReviseItemCall api = new ReviseItemCall(apiContext);
            api.setItemToBeRevised(item);
            FeesType fees = api.reviseItem();

            System.out.println("调用eBay API结束,显示调用结果…");
            System.out.println();

 

 

 

十二、ItemType实例建立以后,咱们调用AddItem API并将新建的ItemType实例做为一个参数传递给这个API。若是调用成功,会返回一个FeesType实例。这个实例包含了登陆商品所涉及的费用。最后,咱们将商品登陆费用和商品ID输出到控制台(在您的应用中, 您能够根据实际需求处理返回结果)。主函数也展现了基本的异常处理,一旦出错,您的应用程序有责任捕获并处理这些异常。
为了屡次测试,能够把 eBayToken 和ServerUrl写死在代码中,省得每次都要去找Token。



 测试结束,发布成功

相关文章
相关标签/搜索