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

搜 索
首页>API开发者平台>技术文档>在Java SDK框架下执行API调用

技术文档

问题
在Java SDK框架下执行API调用
解答
0
人觉得答案有帮助)

总述

   这个范例展示了如何使用eBay Java SDK框架来调用GeteBayOfficialTime指令。使用这个例程的要求是:
   1. 获取你的Sandbox Keys,包括三个ID:DevID, AppID以及CertID,并获取 API Token。
   2. 获取并安装 eBay的Java SDK包

  文末附件是实现GeteBayOfficialTime调用的代码压缩包。


详述 

   创建一个独立的,名为eBayApiRequest,的eBay项目,包含下面的文件结构:  
       1. Java源文件
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/src/ebay/sdk/AppGeteBayTime.java
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/src/ebay/sdk/RequestHeaderParam.java
       2. 构造、批量及属性文件
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/request_param.properties
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/build.xml
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/run.bat 
       3.将你的devid, appid, cert和token放入 request_param.properties文件

       注意:此例程在Sandbox API服务中的测试参见https://api.sandbox.ebay.com/wsapi

   添加Java源代码
   你可以下载项目包,或者手动添加如下的Java代码。
   GeteBayOfficialTime属于简洁的eBay api调用,不含有任何具体的输入。调用成功则返回eBay的官方系统时间。不过当你将此结果输出,则自动转换为当地时间显示。

   例程中用到的devid, appid, cert,  token和apiServer属性存储在request_param.properties的配置文件中,当需要填充ApiCredentia或ApiContext对象时读入辅助类RequestHeaderParam.java中。
   对于API的执行、重试和记录设置都在AppGeteBayTime.java类中完成。

package ebayapi;

import com.ebay.sdk.*;
import com.ebay.sdk.call.*;
import com.ebay.soap.eBLBaseComponents.*;
import com.ebay.sdk.util.eBayUtil;
import com.ebay.sdk.helper.ConsoleUtil;
import java.util.*;  

public class AppGeteBayTime {
          private String configFile= "request_param.properties";
          private ApiContext _apiContext;
          private ApiCall _apiCall;

          AppGeteBayTime(){
                    RequestHeaderParam requestHeaderParam = new  RequestHeaderParam();
                   _apiContext = requestHeaderParam.createContext(configFile);
                   _apiContext.setErrorLanguage("en_US");
                   _apiCall  = new com.ebay.sdk.ApiCall(_apiContext);
                    //Enable logging
                   ApiLogging logging = new ApiLogging();
                  _apiContext.setApiLogging(logging);
          }

          //Main method
          public static void main(String[] args) {
                 AppGeteBayTime thisapp = new AppGeteBayTime();

                 GeteBayOfficialTimeCall request = new GeteBayOfficialTimeCall(thisapp._apiContext);
               
                // Retry
                CallRetry callretry = new CallRetry();
                callretry.setMaximumRetries(3);
                callretry.setDelayTime(1000);

                org.apache.axis.types.Token [] apiErrorCodes =  new org.apache.axis.types.Token[]{
                         new org.apache.axis.types.Token("502"),
                };
               callretry.setTriggerApiErrorCodes(apiErrorCodes);

                java.lang.Class[] tcs = new java.lang.Class[]{
                       java.net.SocketTimeoutException.class,
                      org.apache.axis.AxisFault.class
               };
              callretry.setTriggerExceptions(tcs);
              request.setCallRetry(callretry);
              try {
                    // execute the request and assign the returned eBay time to the Calendar object 
                   Calendar currenttime = request.geteBayOfficialTime();
                   String rightnow = currenttime.getTime().toString();
             }catch (Exception e) {}
       }
} //END OF AppGeteBayTime () class

 

package ebayapi;

import com.ebay.sdk.*;
import com.ebay.soap.eBLBaseComponents.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class RequestHeaderParam {

        public ApiContext createContext(String file){

      // Load the properties file into a Properties object
       Properties config = new Properties();
       try {
              config.load(new FileInputStream(file));
        } catch (IOException e) {
                System.out.println("config.properties file is missing from the current working directory.");
                return null;
        }
        // Create ApiContext object
       ApiContext context = new ApiContext();
       // add ApiCredential to ApiContext
       ApiCredential credential= context.getApiCredential();

       // Register Token property
       credential.seteBayToken(config.getProperty("token") );
       // register the ApiServerUrl in ApiContext
       context.setApiServerUrl(config.getProperty("apiServer"));
       // define the SiteCodeType to US that the request is routed to siteID=0
       context.setSite(SiteCodeType.US);
       // tell eBay api server returning the error message in English
       context.setErrorLanguage("en_US");

       return(context);
    }

}// END OF RequestHeaderParam() class

 

编译和执行例程

编译:
 ant -f build.xml

执行:
 run

执行结果


- Sending SOAP request to: https://api.sandbox.ebay.com/wsapi?siteid=0&callname=
GeteBayOfficialTime&client=java
- HTTP Compression: send gzip request...
- HTTP Compression - gzip decompress response: ContentLength=320
- Request Message: Length=1,575
- <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmln
s:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSc
hema-instance">
 <soapenv:Header>
  <ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ebl="urn:ebay:apis:
eBLBaseComponents">
   <ebl:eBayAuthToken xmlns:ebl="urn:ebay:apis:eBLBaseComponents">AXXX</ebl:eBayAuthToken>
  </ebl:RequesterCredentials>
 </soapenv:Header>
 <soapenv:Body>
  <GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents">
   <ErrorLanguage>en_US</ErrorLanguage>
   <Version>479</Version>
  </GeteBayOfficialTimeRequest>
 </soapenv:Body>
</soapenv:Envelope>
- Response Message: Length=528
- <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmln
s:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSc
hema-instance">
 <soapenv:Body>
  <GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents">


版本信息

上示例程基于此特定的版本号:

API架构版本 549
Java SDK版本 javasdk v549.0 Full release

 

附件

 • ZIP document eBayApiRequest.zip

 

答案对您有帮助吗?

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