【发布时间】:2021-10-03 09:44:22
【问题描述】:
我想向我的 Python 烧瓶 api 发送一个整数数组。我在我的另一个后端 api (Spring Boot) 中编写了这段代码。我从来没有做过这样的事情,所以我不知道如何使用 httpPost 和 CloseableHttpClient 传递 int[] 参数。 我的代码如下:
@PostMapping("/result")
public String getTestResult(@RequestBody Map<String,String> list) throws ClientProtocolException, IOException {
int[] answers = resultService.saveOrUpdate(list);
String stuid = list.get("0");
Optional<Student_Dim> student = studentService.getStudentInformation(Long.parseLong(stuid));
Optional<Location_Dim> location = locationService.getLocation(student.get().getLoc_code());
int[] allData = {answers[0],answers[1],answers[2],answers[3],answers[4],
answers[5],answers[6],answers[7],answers[8],answers[9],
answers[10],answers[11],answers[12],answers[13],answers[14],
answers[15],answers[16],answers[17],answers[18],answers[19],
student.get().getAge(),
location.get().getDensity(),
(int) location.get().getAverage_temp(),
location.get().getInvidualism_score()};
HashMap<String,int[]> mlData = new HashMap<String,int[]>();
mlData.put("data",allData);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://127.0.0.1:5000/personality");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data", allData));
CloseableHttpResponse response = client.execute(httpPost);
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
client.close();
//resultService.insertPersonality(stuid);
String personality = studentService.getPersonality(Long.parseLong(stuid));
return personality;
}
这段代码的重要部分发生在 int[] allData 之后。你可以看到我试过了
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data", allData));
但它说您不能将 String, int[] 传递给 BasicNameValuePair。 我把代码的请求部分贴上来说明清楚:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://127.0.0.1:5000/personality");
//List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//nameValuePairs.add(new BasicNameValuePair("data", allData));
CloseableHttpResponse response = client.execute(httpPost);
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
client.close();
【问题讨论】:
标签: python java spring-boot http-post httprequest