Java實(shí)現(xiàn)京東聯(lián)盟API數(shù)據(jù)獲取功能
一:api參數(shù)
京東聯(lián)盟提供了一個(gè)SDK的包下載好加入到項(xiàng)目中,SDK封裝了api的調(diào)用方法,代碼就是每個(gè)api的調(diào)用實(shí)例如下把申請(qǐng)的四個(gè)參數(shù)填好就行 。
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);加入倆個(gè)依賴(lài)。
<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>是不是按照我上面的來(lái)System.out.println(response),沒(méi)有拿到數(shù)據(jù),就是一串包名 com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsJingfenQueryResponse@1df82230不要慌,因?yàn)槭窃贘ava中集合是打印不出來(lái)的,只需要轉(zhuǎn)化為string就能出來(lái)數(shù)據(jù)。這樣String json = JSON.toJSONString(response),就出來(lái)數(shù)據(jù)了咯。
二:在一個(gè)util里寫(xiě)一個(gè)httpclient方法
public static String doGet(String url) {
// 創(chuàng)建Httpclient對(duì)象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 創(chuàng)建uri
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
// 創(chuàng)建http GET請(qǐng)求
HttpGet httpGet = new HttpGet(uri);
// 執(zhí)行請(qǐng)求
response = httpclient.execute(httpGet);
// 判斷返回狀態(tài)是否為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是入?yún)?shù)前端傳入的就必須是網(wǎng)址格式先調(diào)用getBusiness,在傳參,method是地址,例jd.union.open.activity.query我用的是一個(gè)URL拼接的方法。詳情https://jos.jd.com/commontools?id=2,appKey,appSecret寫(xiě)好。
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");//設(shè)置日期格式
String date = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時(shí)間,也可使用當(dāng)前時(shí)間戳
String rc =getBusinessnot(business);//把入?yún)?shù)轉(zhuǎn)化成json格式
String sj =getTime(date);//把時(shí)間轉(zhuǎn)化網(wǎng)址格式
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;
}
}這樣就拿到了京東聯(lián)盟的數(shù)據(jù)。
有一個(gè)毒點(diǎn),我用帶參的httpclient方法map<string,string> param =new treemap<>();,而不是寫(xiě)一長(zhǎng)串url,列如param.put("360buy_param_json","{\"goodsReq\":{\"eliteId\":\"1\"}}");我試了傳過(guò)去報(bào)json轉(zhuǎn)化異常,不知道咋解決。
到此這篇關(guān)于Java獲取京東聯(lián)盟API數(shù)據(jù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot 圖片上傳與顯示功能實(shí)例詳解
這篇文章主要介紹了spring boot 圖片上傳與顯示功能實(shí)例詳解,需要的朋友可以參考下2017-04-04
spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例
本篇文章主要介紹了spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
提交gRPC-spring-boot-starter項(xiàng)目bug修復(fù)的pr說(shuō)明
這篇文章主要介紹了這篇文章主要為大家介紹了gRPC-spring-boot-starter項(xiàng)目提交bug修復(fù)的pr的原因說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
使用Maven搭建Hadoop開(kāi)發(fā)環(huán)境
這篇文章主要介紹了使用Maven搭建Hadoop開(kāi)發(fā)環(huán)境的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

