注意:以下文档只适用于TOP接口,请谨慎使用!

文档中心 > 基础技术

Java SDK使用说明

更新时间:2016/03/10 访问次数:216498

环境依赖

使用示例

获取淘宝当前系统时间

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());

监听实时消息通知

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/");

批量调用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());

服务地址

API服务地址

环境 HTTP请求地址 HTTPS请求地址
正式环境 http://gw.api.taobao.com/router/rest https://eco.taobao.com/router/rest
海外正式环境 http://api.taobao.com/router/rest https://api.taobao.com/router/rest
沙箱环境 http://gw.api.tbsandbox.com/router/rest https://gw.api.tbsandbox.com/router/rest

消息服务地址

环境 服务地址 SSL服务地址
正式环境 ws://mc.api.taobao.com/
沙箱环境 ws://mc.api.tbsandbox.com/

高级功能

不解释响应字符串为对象(这时候XxxResponse包含的对象为null)

DefaultTaobaoClient.setNeedEnableParser(false)

采用精简化的JSON结构返回,去除多余JSON节点

DefaultTaobaoClient.setUseSimplifyJson(true)

取消API调用日志打点

DefaultTaobaoClient.setNeedEnableLogger(false)

忽略HTTPS证书检查(建议只在测试环境打开)

DefaultTaobaoClient.setIgnoreSSLCheck(true)

取消响应GZIP压缩功能(GZIP压缩功能可以显著的减少网络传输,强烈建议不要取消)

DefaultTaobaoClient.setUseGzipEncoding(false)

设置HTTP连接超时和读超时时间(网络环境差的情况下可以适当增大)

// HTTP连接默认超时时间为:3秒
// HTTP响应读默认超时时间为:15秒
DefaultTaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "appkey", "appsecret", connectTimeout, readTimeout)

API调用出错自动重试(一般情况下ISP类的错误是可以重试成功的)

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调用就近路由(根据API发起调用所在地选择就近的TOP机房进行调用)

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());

注意事项

  • TaobaoClient的实现类都是线程安全的,所以没有必要每次API请求都新建一个TaobaoClient实现类
  • 创建TaobaoClient实现类的实例时,指定format=json,相比xml格式,可以减少数据传输量,提升API请求效率

FAQ

关于此文档暂时还没有FAQ
返回
顶部