Java实现京东联盟API数据获取功能
一:api参数
京东联盟提供了一个SDK的包下载好加入到项目中,SDK封装了api的调用方法,代码就是每个api的调用实例如下把申请的四个参数填好就行 。
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);
加入俩个依赖。
<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>
是不是按照我上面的来System.out.println(response),没有拿到数据,就是一串包名 com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsJingfenQueryResponse@1df82230不要慌,因为是在Java中集合是打印不出来的,只需要转化为string就能出来数据。这样String json = JSON.toJSONString(response),就出来数据了咯。
二:在一个util里写一个Httpclient方法
public static String doGet(String url) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (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;
}
business是入参参数前端传入的就必须是网址格式先调用getBusiness,在传参,method是地址,例jd.union.open.activity.query
我用的是一个URL拼接的方法。详情https://jos.jd.com/commontools?id=2,appKey,appSecret写好。
private static String appKey="";
private static String appSecret="";
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;
}
}
这样就拿到了京东联盟的数据。
有一个毒点,我用带参的httpclient方法map<string,string> param =new treemap<>();,而不是写一长串url,列如param.put("360buy_param_json","{\"goodsReq\":{\"eliteId\":\"1\"}}");我试了传过去报json转化异常,不知道咋解决。
到此这篇关于Java获取京东联盟API数据的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。
相关文章