标签:
有过了解的都知道,虾米被阿里收购了,其所有的对外开放的API都在淘宝开放平台可以看到,看到这里,你如果直接就上淘宝开放平台上注册个应用然后开始测试信源,你会发现行不通!为什么呢?
虾米的对外API并不是对所有人都开放的,他需要以公司的身份进行申请审核,通过后方才可以开通虾米音乐模块的API给你,所以第一步该怎么做!走商务流程,当然这不是我们程序员该做的事情,等你们公司的商务人员沟通好后,那么有权限了就可以开始开发了。
首先你得先申请一个Appkey
如果出现了这个界面,说明商务流程已经走好了,接下来就是你的事了!
这个是接口文档 http://open.taobao.com//doc/api_cat_detail.htm?scope_id=11421&category_id=102
接下来会说明如果使用虾米的接口,请求获取数据!当然你也可以直接看官方的文档,我分享的是经过我改造过后的,废话不多说上代码:
/** * 虾米AppKey */ private static final String APPKEY = "XXXXXXX"; /** * 虾米AppappSecret */ private static final String APPSECRET = "XXXXXX"; /** * 虾米基础url */ private static final String XIAMI_BASE_URL = "http://gw.api.taobao.com/router/rest"; /** * 虾米音乐歌曲搜索接口(搜索整个曲库) */ private static final String M_XIAMI_SEARCH_SONG = "alibaba.xiami.api.search.songs.get"; /** * 虾米榜单歌曲搜索接口 */ private static final String M_XIAMI_BANGDAN_SONG = "alibaba.xiami.api.rank.songs.get"; /** * 虾米音乐歌曲详情接口 */ private static final String M_XIAMI_DETAIL_SONG = "alibaba.xiami.api.song.detail.get";
//参数分别对应上面的常量及接口所需的参数params,这个需要根据不同接口传不同的参数,具体看文档 //这个方法只是辅助方法不会直接调用到, public static String getXiaMiUrl(String xiamiUrl, String methodName, String appkey, String secret, TreeMap<String, String> params) { TreeMap<String, String> apiparamsMap = new TreeMap<String, String>(); if (params != null) { apiparamsMap.putAll(params); } apiparamsMap.put("format", "json"); apiparamsMap.put("method", methodName); apiparamsMap.put("sign_method", "md5"); apiparamsMap.put("app_key", appkey); apiparamsMap.put("v", "2.0"); String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date()); apiparamsMap.put("timestamp", timestamp); // 生成签名 String sign = md5Signature(apiparamsMap, secret); apiparamsMap.put("sign", sign); StringBuilder param = new StringBuilder(); for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, String> e = it.next(); param.append("&").append(e.getKey()).append("=").append( e.getValue()); } return param.toString().substring(1); }
/** * * 新的md5签名,首尾放secret。 * * @param secret * 分配给您的APP_SECRET */ public static String md5Signature(TreeMap<String, String> params, String secret) { String result = null; StringBuffer orgin = getBeforeSign(params, new StringBuffer(secret)); if (orgin == null) return result; orgin.append(secret); try { MessageDigest md = MessageDigest.getInstance("MD5"); result = byte2hex(md.digest(orgin.toString().getBytes("utf-8"))); } catch (Exception e) { throw new java.lang.RuntimeException("sign error !"); } return result; }
/** * * 添加参数的封装方法 */ private static StringBuffer getBeforeSign(TreeMap<String, String> params, StringBuffer orgin) { if (params == null) return null; Map<String, String> treeMap = new TreeMap<String, String>(); treeMap.putAll(params); Iterator<String> iter = treeMap.keySet().iterator(); while (iter.hasNext()) { String name = (String) iter.next(); orgin.append(name).append(params.get(name)); } return orgin; }
/** * * 二行制转字符串 */ private static String byte2hex(byte[] b) { StringBuffer hs = new StringBuffer(); String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) hs.append("0").append(stmp); else hs.append(stmp); } return hs.toString().toUpperCase(); }
/** 连接到TOP服务器并获取数据 */ /** * 这个方法就是你自己调用的方法其他的方法都是辅助方法,urlStr是基础路径 */ public static String getXiaMiResult(String urlStr, String methodName, TreeMap<String, String> params) { String content = getXiaMiUrl(urlStr, methodName, APPKEY,APPSECRET, params); /** 连接到TOP服务器并获取数据 */ URL url = null; HttpURLConnection connection = null; try { url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.connect(); DataOutputStream out = new DataOutputStream(connection .getOutputStream()); out.write(content.getBytes("utf-8")); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection .getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } return null; }
好了就到此结束了,仅供参考,希望可以帮到你!
标签:
原文地址:http://my.oschina.net/pingdy/blog/381176