Java调用淘宝API demo源代码
【摘要】
/** * */ package com.taobao.api.demo.onsale; import java.io.IOException; import&nb...
- /**
- *
- */
- package com.taobao.api.demo.onsale;
- import java.io.IOException;
- import java.net.URLEncoder;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.taobao.api.TaobaoApiException;
- import com.taobao.api.TaobaoJsonRestClient;
- import com.taobao.api.TaobaoRestClient;
- import com.taobao.api.model.ItemsGetResponse;
- import com.taobao.api.model.ItemsOnSaleGetRequest;
- /**
- * @version 2008-10-29
- * @author <a href="mailto:zixue@taobao.com">zixue</a>
- *
- */
- public class MainServlet extends HttpServlet {
- private static final transient Log log = LogFactory
- .getLog(MainServlet.class);
- /**
- *
- */
- private static final long serialVersionUID = 8839099738909766274L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String appkey = req.getParameter("appkey");
- String secret = req.getParameter("secret");
- if (log.isDebugEnabled()) {
- log.debug(appkey + ":" + secret);
- }
- if (appkey != null && secret != null) {
- try {
- //生成客户端实例
- TaobaoRestClient client = new TaobaoJsonRestClient(appkey,secret);
- //淘宝卖家的出售的商品列表
- ItemsOnSaleGetRequest tbReq = new ItemsOnSaleGetRequest()
- .withFields("iid,title,price,list_time,num")
- .withPageNo(1)
- .withPageSize(20);
- // use appkey as session_id
- ItemsGetResponse tbRsp = client.itemsOnSaleGet(tbReq, appkey);//搜索单个商品信息的Request请求
- if (log.isDebugEnabled()) {
- log.debug(tbRsp.getBody());
- }
- //如果接口调用成功
- if (!tbRsp.isSuccess()) {
- if (tbRsp.isRedirect()) {
- req.setAttribute("url",
- tbRsp.getRedirectUrl(URLEncoder
- .encode
- (req.getRequestURL()
- .append('?')
- .append("appkey=")
- .append(appkey)
- .append("&secret=")
- .append(secret)
- .toString(),"utf-8")
- )
- );
- } else {
- req.setAttribute("msg", tbRsp.getMsg());
- }
- } else {
- req.setAttribute("items", tbRsp.getItems());
- }
- } catch (TaobaoApiException e) {
- throw new ServletException(e);
- }
- }
- req.getRequestDispatcher("main.jsp").forward(req, resp);
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- }
视频课堂:https://edu.csdn.net/course/play/8222
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/24596953
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)