gpt4 book ai didi

java - 使用java进行类似于ajax的调用

转载 作者:行者123 更新时间:2023-12-02 04:36:40 26 4
gpt4 key购买 nike

我经历过这个Previous post关于如何在 java 中使用 ajax 进行等效调用。如果请求返回 json,如何检索响应。

final URL url = new URL("http://localhost:8080/SearchPerson.aspx/PersonSearch");
final URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
urlConnection.connect();
final OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(("{\"fNamn\": \"" + stringData + "\"}").getBytes("UTF-8"));
outputStream.flush();
final InputStream inputStream = urlConnection.getInputStream();`

最佳答案

试试这个:

StringBuffer jsonBuffer = new StringBuffer();
BufferedReader reader = null;
String line = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null)
jsonBuffer.append(line);
} catch (Exception e) {
//Handle error
}
finally {
reader.close();
}
String json = jsonBuffer.toString();

关于java - 使用java进行类似于ajax的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30662533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com