Platform Technology-Java SDK Instructions
Environment dependencies
- Java SE/EE 1.5 and above (Android platform not supported)
- Apache Commons Logging
Usage example
Get Taobao’s current system time
DefaultTaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret"); TimeGetRequest request = new TimeGetRequest(); TimeGetResponse response = client.execute(request); if (response.isSuccess()) { System.out.println(response.getBody()); }
Get single transaction details
DefaultTaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret"); TradeFullinfoGetRequest req = new TradeFullinfoGetRequest(); req.setFields("tid,type,status,payment,orders"); req.setTid(123456789L); TradeFullinfoGetResponse rsp = client.execute(req, sessionKey); System.out.println(rsp.getBody());
Listen to real-time message notifications
TmcClient client = new TmcClient("app_key", "app_secret", "default"); client.setMessageHandler(new MessageHandler() { public void onMessage(Message message, MessageStatus status) { try { System.out.println(message.getContent()); System.out.println(message.getTopic()); } catch (Exception e) { e.printStackTrace(); status.fail();// 消息处理失败回滚,服务端需要重发 } } }); client.connect("ws://mc.api.taobao.com/");
Batch call API
BatchTaobaoClient client = new BatchTaobaoClient("http://gw.api.taobao.com/router/batch", "appkey", "appsecret"); TaobaoBatchRequest batch = new TaobaoBatchRequest(); TimeGetRequest timeRequest = new TimeGetRequest(); AppipGetRequest ipRequest = new AppipGetRequest(); batch.addRequest(timeRequest).addRequest(ipRequest); TaobaoBatchResponse response = client.execute(batch); System.out.println(response.getBody());
Service address
API service address
Message service address
Advanced functions
Do not interpret the response string as an object (the object contained in XxxResponse is null at this time)
DefaultTaobaoClient.setNeedEnableParser(false)
Use a simplified JSON structure to return and remove redundant JSON nodes
DefaultTaobaoClient.setUseSimplifyJson(true)
Cancel API call log management
DefaultTaobaoClient.setNeedEnableLogger(false)
Ignore HTTPS certificate check (recommended to be turned on only in test environment)
DefaultTaobaoClient.setIgnoreSSLCheck(true)
Cancel response to GZIP compression function (GZIP compression function can significantly reduce Network transmission, it is strongly recommended not to cancel)
DefaultTaobaoClient.setUseGzipEncoding(false)
Set the HTTP connection timeout and read timeout (can be increased appropriately if the network environment is poor)
// HTTP连接默认超时时间为:3秒 // HTTP响应读默认超时时间为:15秒 DefaultTaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret", connectTimeout, readTimeout)
API call Automatically retry when errors occur (generally, ISP errors can be retried successfully)
AutoRetryTaobaoClient client = new AutoRetryTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret"); client.setMaxRetryCount(3); client.setRetryWaitTime(100L); TimeGetRequest request = new TimeGetRequest(); TimeGetResponse response = client.execute(request); if (response.isSuccess()) { System.out.println(response.getBody()); }
API call nearest routing (select the nearest TOP computer room to call based on the location where the API call is initiated)
ClusterTaobaoClient client = new ClusterTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret"); TimeGetRequest request = new TimeGetRequest(); TimeGetResponse response = client.execute(request); System.out.println(response.getBody());
Notes
- TaobaoClient implementation classes are all thread-safe, so there is no need to create a new TaobaoClient implementation class for every API request
- When creating an instance of the TaobaoClient implementation class, specify format =json, compared to xml format, it can reduce the amount of data transmission and improve the efficiency of API requests
FAQ
- There is no FAQ about this document