标签:java,String,iterator,list,RestTemplate,params,println,new,post From: https://www.cnblogs.com/Lee597/p/18231366
public void 方法名() {
RestTemplate restTemplate = new RestTemplate();
String url = "http://目标地址";
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("post传输key", "post传输value");
params.add("key", "value");
params.add("键", "值");
HttpHeaders headers = new HttpHeaders();//设置请求头
headers.set("键key", "值value");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);//传输对象
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);//接收返回结果
String body = response.getBody();
if (body != null) {
fixString(body);//解析方法
}
}
//解析方法: 根据个人需求解析就好
private void fixString(String str) {
List<String> list = new ArrayList<>();
for (String s : str.split(",")) {
list.addAll(Arrays.asList(s.split("\"")));
}
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
if (s.equals("{")) {
iterator.remove();
}
if (s.equals("}")) {
iterator.remove();
}
if (s.equals(":")) {
iterator.remove();
}
}
try {
String access_token = null;
// JSONArray jsonArray = new JSONArray();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("需求提取字段")) {
JSONObject jsonObject = new JSONObject();
存储字段 = "bearer " + list.get(i + 1);
jsonObject.put("存储字段", 存储内容);
// jsonArray.put(jsonObject);
System.out.println("----------------------------------------------------");
System.out.println(jsonObject);
System.out.println("----------------------------------------------------");
}
}
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}