Browse Source

修正get请求方式

master
xyiege 3 years ago
parent
commit
8f5cd72dea
  1. 11
      src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java
  2. 2
      src/main/java/cn/chjyj/szwh/service/impl/LoginServiceImpl.java
  3. 2
      src/main/java/cn/chjyj/szwh/service/impl/UserServiceImpl.java
  4. 8
      src/main/java/cn/chjyj/szwh/utils/RequestUtils.java
  5. 6
      src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java
  6. 2
      src/test/java/cn/chjyj/szwh/ApiTest.java

11
src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java

@ -84,12 +84,13 @@ public class GoodsServiceImpl implements GoodsService {
Map<String, Object> hmap = new HashMap();
hmap.put("entrust_token", sign);
// get 方式获取
JSONObject jsonObject = RequestUtils.GetData(apiurl, hmap);
// json的data节点
JSONArray jsonArray = jsonObject.getJSONArray("data");
JSONObject jsonObject = RequestUtils.GetData(apiurl, hmap,true);
// 产品详情地址
String gdurl = host + "/consign/exchange/v1/selectEntrustDetails";
// 循环查询商品的详情
if(jsonObject!=null && jsonObject.containsKey("data") ) {
// json的data节点
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.size(); i++) {
// 获取到sourceData字段信息
JSONObject sdjson = (JSONObject) jsonArray.get(i);
@ -103,7 +104,7 @@ public class GoodsServiceImpl implements GoodsService {
// 以identifier为主键查询详情
String gdetailUrl = gdurl + "/" + goodsId;
// 请求商品的详情
JSONObject gdJson = RequestUtils.GetData(gdetailUrl, hmap);
JSONObject gdJson = RequestUtils.GetData(gdetailUrl, hmap,true);
log.info(goodsId + "详情:" + gdJson);
//处理返回的结果
JSONArray gdArr = gdJson.getJSONArray("data");
@ -172,7 +173,7 @@ public class GoodsServiceImpl implements GoodsService {
// 计算器增加1
count++;
}
}
return count;
}

2
src/main/java/cn/chjyj/szwh/service/impl/LoginServiceImpl.java

@ -45,7 +45,7 @@ public class LoginServiceImpl implements LoginService {
hmap.put("Content-Type","application/json");
//执行请求
JSONObject jsonObject = RequestUtils.GetData(path,hmap);
JSONObject jsonObject = RequestUtils.GetData(path,hmap,true);
log.debug("request result:"+jsonObject.toString());
String code = jsonObject.getString("resultCode");

2
src/main/java/cn/chjyj/szwh/service/impl/UserServiceImpl.java

@ -68,7 +68,7 @@ public class UserServiceImpl implements UserService {
//hmap.put("Content-Type","application/json");
//执行请求
JSONObject json = RequestUtils.GetData(userUrl,hmap);
JSONObject json = RequestUtils.GetData(userUrl,hmap,true);
log.info("request result:"+json);
// json 内容转为map
Map rmap = JSONObject.parseObject(json.getString("data"));

8
src/main/java/cn/chjyj/szwh/utils/RequestUtils.java

@ -31,9 +31,10 @@ public class RequestUtils {
* 能设置请求头的get方式
* @param surl
* @param map
* @param wflag 是否写入json文件
* @return
*/
public static JSONObject GetData(String surl, Map<String,Object> map){
public static JSONObject GetData(String surl, Map<String,Object> map,boolean wflag){
JSONObject jsonObject = new JSONObject();
try{
URL url = new URL(surl);
@ -51,13 +52,16 @@ public class RequestUtils {
InputStream inputStream = httpcon.getInputStream();
//请求结果写入
String filename =String.valueOf(Calendar.getInstance().getTimeInMillis());
if(wflag){
SzFileUtils.createFile(inputStream,filename);
//字节转为utf-8
String message=SzFileUtils.readJson(filename);
if(message!=null){
jsonObject = JSONObject.parseObject(message);
}
logger.info("respone message :"+message);
}
logger.info("respone message is null");
}
}catch (IOException ex){
// 罗列出明细错误
logger.error("请求错误:"+ex);

6
src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java

@ -37,7 +37,7 @@ public class SzwhApiUtils {
hmap.put("token",sign);
hmap.put("Content-Type","application/json");
//执行请求
JSONObject json = RequestUtils.GetData(userUrl,hmap);
JSONObject json = RequestUtils.GetData(userUrl,hmap,false);
log.info("request result:"+json);
return json;
}
@ -78,7 +78,7 @@ public class SzwhApiUtils {
System.out.println("token :"+sign);
Map hmap = new HashMap();
hmap.put("entrust_token",sign);
JSONObject json = RequestUtils.GetData(url,hmap);
JSONObject json = RequestUtils.GetData(url,hmap,false);
return json;
}
@ -94,7 +94,7 @@ public class SzwhApiUtils {
String sign=SignUtils.createSign("entrust");
Map hmap = new HashMap();
hmap.put("entrust_token",sign);
JSONObject json = RequestUtils.GetData(url,hmap);
JSONObject json = RequestUtils.GetData(url,hmap,true);
return json;
}
/**

2
src/test/java/cn/chjyj/szwh/ApiTest.java

@ -64,7 +64,7 @@ public class ApiTest {
// 请求头
Map<String, Object> hmap = new HashMap();
hmap.put("entrust_token", sign);
JSONObject jsonObject = RequestUtils.GetData(url, hmap);
JSONObject jsonObject = RequestUtils.GetData(url, hmap,true);
// 抽取data
// String data = jsonObject.getString("data");
JSONArray jsonArray = jsonObject.getJSONArray("data");

Loading…
Cancel
Save