gpt4 book ai didi

Java Http 请求 JSON 和响应处理

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:19 25 4
gpt4 key购买 nike

我已经看过其他几个问题,但我仍然没有完全理解这一点。我想将 JSON 字符串发布到远程地址,然后从 JSON 响应中检索值。我正在使用适用于 Java 的 Apache 库。

public HttpResponse http(String url, String body) throws IOException {

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
//httpClient.execute(request);
HttpResponse result = httpClient.execute(request);

} catch (IOException ex) {
}
return null;
}

作为正文,我将传递以下内容(示例):

{"example":1,"fr":"lol"}

我也完全不知道如何从响应中检索 JSON 值。

最佳答案

最简单的方法是使用像 google-http-java-client 这样的库但是如果你想自己解析 JSON 响应,你可以通过多种方式来解析,你可以使用 org.json , json-simple , Gson , minimal-json , jackson-mapper-asl (从 1.x 开始)...等等

一组简单的例子:

使用 Gson:

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class Gson {

public static void main(String[] args) {
}

public HttpResponse http(String url, String body) {

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse result = httpClient.execute(request);
String json = EntityUtils.toString(result.getEntity(), "UTF-8");

com.google.gson.Gson gson = new com.google.gson.Gson();
Response respuesta = gson.fromJson(json, Response.class);

System.out.println(respuesta.getExample());
System.out.println(respuesta.getFr());

} catch (IOException ex) {
}
return null;
}

public class Response{

private String example;
private String fr;

public String getExample() {
return example;
}
public void setExample(String example) {
this.example = example;
}
public String getFr() {
return fr;
}
public void setFr(String fr) {
this.fr = fr;
}
}
}

使用 json-simple:

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonSimple {

public static void main(String[] args) {

}

public HttpResponse http(String url, String body) {

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse result = httpClient.execute(request);

String json = EntityUtils.toString(result.getEntity(), "UTF-8");
try {
JSONParser parser = new JSONParser();
Object resultObject = parser.parse(json);

if (resultObject instanceof JSONArray) {
JSONArray array=(JSONArray)resultObject;
for (Object object : array) {
JSONObject obj =(JSONObject)object;
System.out.println(obj.get("example"));
System.out.println(obj.get("fr"));
}

}else if (resultObject instanceof JSONObject) {
JSONObject obj =(JSONObject)resultObject;
System.out.println(obj.get("example"));
System.out.println(obj.get("fr"));
}

} catch (Exception e) {
// TODO: handle exception
}

} catch (IOException ex) {
}
return null;
}
}

等...

关于Java Http 请求 JSON 和响应处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22816335/

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