Entwicklungsdokumente für die offene Taobao-Plattform
/ 平臺(tái)技術(shù)-Java SDK使用說(shuō)明
平臺(tái)技術(shù)-Java SDK使用說(shuō)明
環(huán)境依賴
- Java SE/EE 1.5及以上(不支持Android平臺(tái))
- Apache Commons Logging
使用示例
獲取淘寶當(dāng)前系統(tǒng)時(shí)間
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()); }
獲取單筆交易詳情
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());
監(jiān)聽實(shí)時(shí)消息通知
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();// 消息處理失敗回滾,服務(wù)端需要重發(fā) } } }); client.connect("ws://mc.api.taobao.com/");
批量調(diào)用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());
服務(wù)地址
API服務(wù)地址
消息服務(wù)地址
高級(jí)功能
不解釋響應(yīng)字符串為對(duì)象(這時(shí)候XxxResponse包含的對(duì)象為null)
DefaultTaobaoClient.setNeedEnableParser(false)
采用精簡(jiǎn)化的JSON結(jié)構(gòu)返回,去除多余JSON節(jié)點(diǎn)
DefaultTaobaoClient.setUseSimplifyJson(true)
取消API調(diào)用日志打點(diǎn)
DefaultTaobaoClient.setNeedEnableLogger(false)
忽略HTTPS證書檢查(建議只在測(cè)試環(huán)境打開)
DefaultTaobaoClient.setIgnoreSSLCheck(true)
取消響應(yīng)GZIP壓縮功能(GZIP壓縮功能可以顯著的減少網(wǎng)絡(luò)傳輸,強(qiáng)烈建議不要取消)
DefaultTaobaoClient.setUseGzipEncoding(false)
設(shè)置HTTP連接超時(shí)和讀超時(shí)時(shí)間(網(wǎng)絡(luò)環(huán)境差的情況下可以適當(dāng)增大)
// HTTP連接默認(rèn)超時(shí)時(shí)間為:3秒 // HTTP響應(yīng)讀默認(rèn)超時(shí)時(shí)間為:15秒 DefaultTaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret", connectTimeout, readTimeout)
API調(diào)用出錯(cuò)自動(dòng)重試(一般情況下ISP類的錯(cuò)誤是可以重試成功的)
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調(diào)用就近路由(根據(jù)API發(fā)起調(diào)用所在地選擇就近的TOP機(jī)房進(jìn)行調(diào)用)
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());
注意事項(xiàng)
- TaobaoClient的實(shí)現(xiàn)類都是線程安全的,所以沒(méi)有必要每次API請(qǐng)求都新建一個(gè)TaobaoClient實(shí)現(xiàn)類
- 創(chuàng)建TaobaoClient實(shí)現(xiàn)類的實(shí)例時(shí),指定format=json,相比xml格式,可以減少數(shù)據(jù)傳輸量,提升API請(qǐng)求效率
FAQ
- 關(guān)于此文檔暫時(shí)還沒(méi)有FAQ