Java實現(xiàn)京東聯(lián)盟API數(shù)據(jù)獲取功能
一:api參數(shù)
京東聯(lián)盟提供了一個SDK的包下載好加入到項目中,SDK封裝了api的調(diào)用方法,代碼就是每個api的調(diào)用實例如下把申請的四個參數(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);加入倆個依賴。
<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),沒有拿到數(shù)據(jù),就是一串包名 com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsJingfenQueryResponse@1df82230不要慌,因為是在Java中集合是打印不出來的,只需要轉(zhuǎn)化為string就能出來數(shù)據(jù)。這樣String json = JSON.toJSONString(response),就出來數(shù)據(jù)了咯。
二:在一個util里寫一個httpclient方法
public static String doGet(String url) {
// 創(chuàng)建Httpclient對象
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請求
HttpGet httpGet = new HttpGet(uri);
// 執(zhí)行請求
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我用的是一個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");//設(shè)置日期格式
String date = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間,也可使用當(dāng)前時間戳
String rc =getBusinessnot(business);//把入?yún)?shù)轉(zhuǎn)化成json格式
String sj =getTime(date);//把時間轉(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ù)。
有一個毒點,我用帶參的httpclient方法map<string,string> param =new treemap<>();,而不是寫一長串url,列如param.put("360buy_param_json","{\"goodsReq\":{\"eliteId\":\"1\"}}");我試了傳過去報json轉(zhuǎn)化異常,不知道咋解決。
到此這篇關(guān)于Java獲取京東聯(lián)盟API數(shù)據(jù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
【Java】BigDecimal實現(xiàn)加減乘除運算代碼
本篇文章主要介紹了【Java】BigDecimal實現(xiàn)加減乘除運算代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
SpringBoot使用Spring Test進行集成測試的流程步驟
Spring Test 是 Spring Framework 提供的一個測試框架,它可以幫助我們進行集成測試,在本文中,我們將介紹如何使用 Spring Test 進行集成測試,需要的朋友可以參考下2023-06-06
mybatis Invalid bound statement(not foun
這篇文章主要介紹了mybatis Invalid bound statement(not found)排坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
MybatisPlus分頁查詢與多條件查詢介紹及查詢過程中空值問題的解決
mybatisplus是個很好用的插件,相信小伙伴們都知道,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus實現(xiàn)分頁查詢與多條件查詢介紹及查詢過程中空值問題的相關(guān)資料,需要的朋友可以參考下2022-10-10
@Async導(dǎo)致controller?404及失效原因解決分析
這篇文章主要為大家介紹了@Async導(dǎo)致controller?404失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java使用httpRequest+Jsoup爬取紅藍(lán)球號碼
本文將結(jié)合實例代碼,介紹Java使用httpRequest+Jsoup爬取紅藍(lán)球號碼,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Spring @Bean vs @Service注解區(qū)別
本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

