eBay大中华区API开发者平台 开发者专区

搜 索
首页>API开发者平台>技术文档>Java例程: 通过HTTP posting Trading API Request XML to eBay trading API web service and JAXB framework f

技术文档

问题
Java例程: 通过HTTP posting Trading API Request XML to eBay trading API web service and JAXB framework f
解答
0
人觉得答案有帮助)

总述  

  此Java例程演示了使用HttpURLConnection向for posting Trading API Request XML to eBay trading API web service and JAXB framework for Java-XML映射。 
 

  文末附件包含两个应用实例:AddFixedPriceItemCall 和 getSellerListCall


软件需求  
===================== 
1. JAXWS2.1.5 或更高版本
      - 设置JWSDP_HOME系统变量,指向你的JAXWS2.1.5安装目录。
2.JDK 1.6 或更高版本  


指令
============

A. 连接Trading API Schema 
     1. 下载新版本的Trading API XSD文件,保存在 ${Trading_XML_Client_HOME}/XSD 文件夹
     2. 执行jaxb-codeGen.xml脚本,生成可代表步骤1中保存的Trading API Schema的Java类
           > ant -f  ${Trading_XML_Client_HOME}/jaxb-codeGen.xml 

B.  构建并运行例程
     
    1. 构建并运行应用例程:AddFixedPriceItemCall  
       1.1 打开文件夹:${Trading_XML_Client_HOME}/samples/AddFixedPriceItemCall
       1.2 在Configruation.xml文件中指定你的Sandbox user auth token 
       1.3 对此进行必要的修改:${Trading_XML_Client_HOME}/samples/AddFixedPriceItemCall/src/com/ebay/sample/AddFixedPriceItemCall.java 
       1.4 执行ant脚本,在Sandbox环境下刊登一个商品   
       > ant -f build.xml 
     
    2. 构建并运行应用例程:GetSellerListCall
      1.1 打开文件夹:${Trading_XML_Client_HOME}/samples/GetSellerListCall
      1.2在Configruation.xml文件中指定你的Sandbox user auth token 
      1.3 对此进行必要的修改:${Trading_XML_Client_HOME}/samples/GetSellerListCall/src/com/ebay/sample/GetSellerListCall.java 
      1.4 执行ant脚本,检索你的active item list 
       > ant -f build.xml


 实施细节

1. 创建一个Request类型的对象,如GetSellerListRequestType

    // create and initial an GetSellerListRequestType object  
    GetSellerListRequestType request = new GetSellerListRequestType();  
    XMLRequesterCredentialsType requestCredential = new XMLRequesterCredentialsType();  
    requestCredential.setEBayAuthToken(sample.USERTOKEN);  
    request.setRequesterCredentials(requestCredential);  
    java.util.Calendar nowCal = java.util.Calendar.getInstance();  
    java.util.Calendar fromCal = (java.util.Calendar) nowCal.clone();  
    XMLGregorianCalendar xmlgregorianTo = toApiTime(nowCal);  
    XMLGregorianCalendar xmlgregorianFrom = fromApiTime(fromCal,24);  

    request.setErrorLanguage("en_US");  
    request.setIncludeWatchCount(Boolean.TRUE);  
    request.setStartTimeFrom(xmlgregorianFrom);  
    request.setStartTimeTo(xmlgregorianTo);

 

2. 将JAXB对象(此例程中是GetSellerListRequestType)转换为XML Request文档

         /* 
          *  marshal GetSellerListRequestType object to XML data  file 
          * 
          */ 
         private String marshalObject2Xml(GetSellerListRequestType requestobj) throws Exception { 
             String requestXmlFileName = "GetSellerListRequest.xml"; 
             jc = JAXBContext.newInstance(JAXB_PACKAGE_NAME); 
             Marshaller m = jc.createMarshaller(); 
             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
             ObjectFactory of = new ObjectFactory(); 

             JAXBElement<GetSellerListRequestType> requestElement = of.createGetSellerListRequest(requestobj); 
             m.marshal(requestElement, new FileWriter(requestXmlFileName)); 
             return requestXmlFileName; 
         }

3. 创建HttpsURLConnection对象,指定所需的Trading API HTTP Headers 并将生成的XML Request文档发送至Trading API URL ( Sandbox : https://api.sandbox.ebay.com/ws/api.dll).

         /* 
          * open SSL https connection and send the request xml data 
          * 
          */ 
         public InputStreamReader openConnectionFireRequest(String XmlFileName, String ApiCallName) throws Exception { 
             URL url = new URL(SERVERURL); 
             // open SSL https connection 
             HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
             conn.setRequestMethod("POST"); 

             conn.setDoInput(true); 
             conn.setDoOutput(true); 
             conn.setRequestProperty("Content-Type", "text/xml"); 
             // required headers 
             conn.setRequestProperty("X-EBAY-API-COMPATIBILITY-LEVEL", "687"); 
             conn.setRequestProperty("X-EBAY-API-CALL-NAME", ApiCallName); 
             conn.setRequestProperty("X-EBAY-API-SITEID", "0"); 

             PrintWriter output = new PrintWriter(new OutputStreamWriter(conn.getOutputStream())); 
             Thread.sleep(1000); 
             String fileContent = convertFileContent2String(XmlFileName); 

             System.out.println(conn.getURL()); 
             System.out.println(fileContent); 
             output.println(fileContent); 
             output.close(); 
             conn.connect(); 
             InputStreamReader isr = new InputStreamReader(conn.getInputStream()); 
             return isr; 
         }

 

4. 读取HttpsURLConnection中返回的输入流。GetInputStream()到XML字符串,然后将XML文档字符串逆向转换为Java Object (此例程中是GetSellerLisResponseType)

           /* 
          * read input stream and unmarshal it into a Java content tree 
          */ 
         public Object unmarshalInputStream(InputStreamReader isr) { 
             Object returnedObj = null; 
             try { 
                 int c = 0; 
                 String addFixedPriceItemResponse = "GetSellerListResponse.xml"; 
                 OutputStream f = new FileOutputStream(addFixedPriceItemResponse); 
                 while ((c = isr.read()) > -1) { 
                     f.write(c); 
                 } 
                 f.close(); 
                 String responseXML = convertFileContent2String(addFixedPriceItemResponse); 
                 System.out.println(responseXML); 
                 InputStream input = new ByteArrayInputStream(responseXML.getBytes()); 
                 JAXBContext context = JAXBContext.newInstance(JAXB_PACKAGE_NAME); 
                 Unmarshaller unmarshaller = context.createUnmarshaller(); 
                 JAXBElement<Object> poe = (JAXBElement) unmarshaller.unmarshal(input); 
                 returnedObj = poe.getValue(); 

                 isr.close(); 
             } catch (Exception e) { 
                //handle exception 
             } 
             return returnedObj; 
         }

 

  附件
   • ZIP document Trading_XML_Client.zip

 

答案对您有帮助吗?

是,对我很有帮助
否,没解决我的问题