提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 一、SDK调用
- 1.把SDK引入项目中
- 2.加入依赖
- 3.代码
- 二、Httpclien调用
- 1.加入依赖
- 2.写一个HttpclienUtil方法
- 3.传参
一、SDK调用
1.把SDK加入项目中
有java,PHP等的版本,需要什么就下载什么,加入到项目bin中。
2.加入依赖
<dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.2</version>
</dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-core-asl</artifactId><version>1.9.2</version>
</dependency>
3.代码
四个参数,accessToken一般是不需要的,后面的就是京东联盟上的调用示例了,自己拷贝就好。自己只需要加入参参数就OK了。
String SERVER_URL = "https://api.jd.com/routerjson";
String accessToken = null;
String appKey = "";
String appSecret = "";
JdClient client=new DefaultJdClient(SERVER_URL,accessToken,appKey,appSecret);
UnionOpenGoodsJingfenQueryRequest request=new UnionOpenGoodsJingfenQueryRequest();
JFGoodsReq goodsReq=new JFGoodsReq();//入参参数
goodsReq.setEliteId(1);
request.setGoodsReq(goodsReq);
request.setVersion("1.0");
UnionOpenGoodsJingfenQueryResponse response=client.execute(request);
System.out.println(response);
提示:System.out.println(response);是打印不出来数据的,要转化为String在打印就可以了
二、Httpclien调用
1.加入依赖
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version>
</dependency>
2.写一个HttpclienUtil方法
public static String doGet(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}
3.传参
我自己试了有参的调用,直接报json转化异常,所以就使用了无参的调用,只传url
public String getGoodsJingfenQuery(String business,String method) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳String rc =getBusinessnot(business);//把入参参数转化成json格式String sj =getTime(date);//把时间转化网址格式String str =appSecret+"360buy_param_json"+rc+"app_key"+appKey+"method"+method+"sign_methodmd5timestamp"+date+"v1.0"+appSecret;String sign=MD5(str);//获取签名,MD5 32位的加密String url="https://api.jd.com/routerjson?360buy_param_json="+newbusiness+"&app_key="+appKey+"&method="+method+"&sign_method=md5×tamp="+sj+"&v=1.0&sign="+sign;return httpClientUtil.doGet(url,null);}
里面的方法
public String getBusiness(String business) {String a = business.replace("{","%7B");String b = a.replace(":","%3A");String c = b.replace("}","%7D");return c.replace("\"","%22");}public String getBusinessnot(String business) {String a = business.replace("%7B","{");String b = a.replace("%3A",":");String c = b.replace("%7D","}");return c.replace("%22","\"");}public String getTime(String time) {String a = time.replace(" ","+");return a.replace(":","%3A");}public String getMD5(String str) {try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(str.getBytes());return new BigInteger(1, md.digest()).toString(16);} catch (Exception e) {e.printStackTrace();return null;}}public static String MD5(String s) {char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};try {byte[] btInput = s.getBytes();MessageDigest mdInst = MessageDigest.getInstance("MD5");mdInst.update(btInput);byte[] md = mdInst.digest();int j = md.length;char str[] = new char[j * 2];int k = 0;for (int i = 0; i < j; i++) {byte byte0 = md[i];str[k++] = hexDigits[byte0 >>> 4 & 0xf];str[k++] = hexDigits[byte0 & 0xf];}return new String(str);} catch (Exception e) {e.printStackTrace();return null;}}