Skip to main content
Published: October 12 2006, 2:43:00 PMUpdated: November 28 2020, 2:52:33 PM

 Using Java SDK framework to execute your first API Call

Summary 

   This sample illustrates on how to use eBay Java SDK framework to execute the GeteBayOfficialTime call.   For using this sample you must have:
   1. got your sandbox keys that include three IDs (DevID, AppID, and CertID) and API Token.
   2. obtained and installed the eBay Java SDK package.

  Attached to this article is a zipped project implementing the GeteBayOfficialTime code.


Detailed Description 

   Creating a standalone eBay Project named eBayApiRequest, which consists the file structure as below.   
       1. Java Source files
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/src/ebay/sdk/AppGeteBayTime.java
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/src/ebay/sdk/RequestHeaderParam.java
       2. Build, batch and properties files
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/request_param.properties
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/build.xml
             ${EBAY_JAVA_SDK_HOME}/eBayApiRequest/run.bat 
       3. Plug your own devid, appid, cert, and token into the request_param.properties file.

       NOTE. This sample has been tested against the sandbox API service https://api.sandbox.ebay.com/wsapi

   Add Java Source Code
   Now you need to write the java code as below or you can download the project package. 
   GeteBayOfficialTime is the simplest eBay api call that has no call-specific inputs. If the call succeeds and it returns the official eBay system time in GMT. However; please keep in mind that when you print the time out, the time will be displayed in a local time.

   Each call to the eBay SOAP API must specify and register a set of the required properties into ApiContext object to pass to the HTTP header,
   In the example, the devid, appid, cert,  token and apiServer properties are stored in the request_param.properties config file and are read into the helper class RequestHeaderParam.java for populating ApiCredential and ApiContext object.
   The api executing, api retrying and logging setting up are all done in the AppGeteBayTime.java class.

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

Compiling and executing the project

Compile :
 ant -f build.xml

Execute:
 run

The Executing result
- 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">


Version Info

The code example above was based on the versions specified below:

API Schema Version 549
Java SDK Version javasdk v549.0 Full release

Additional Resource 

How well did this answer your question?
Answers others found helpful