技术文档
总述
此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)
/* |
附件 | |
• Trading_XML_Client.zip |
答案对您有帮助吗?
是,对我很有帮助 | |
否,没解决我的问题 |