获取HTTP/HTTPS协议返回的JSON数据可以通过Java提供的HttpClient库来实现。以下是完整的攻略:
准备工作
在使用HttpClient库之前,需要先引入该库。可以在Maven项目中添加以下依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
发送HTTP GET请求获取JSON数据
下面是一个示例代码,使用HttpGet方法发送HTTP GET请求获取JSON数据:
import java.io.IOException;
import java.util.Scanner;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HttpJsonGetDemo {
public static void main(String[] args) throws IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
HttpEntity responseEntity = httpClient.execute(request).getEntity();
String responseString = EntityUtils.toString(responseEntity);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(responseString);
int id = node.get("id").asInt();
String title = node.get("title").asText();
boolean completed = node.get("completed").asBoolean();
System.out.println("id: " + id);
System.out.println("title: " + title);
System.out.println("completed: " + completed);
}
}
在这个示例中,我们使用了 HttpClient
发送了一个 HttpGet
请求,并获取返回数据后将其解析成一个 JsonNode
对象。
发送HTTPS GET请求获取JSON数据
如果需要访问HTTPS站点,需要对 HttpClient
进行配置。以下示例展示如何配置并发送HTTP GET请求:
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HttpsJsonGetDemo {
public static void main(String[] args) throws Exception {
HttpClient httpClient = HttpClients.custom()
.setSSLContext(SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
return true;
}
}).build())
.build();
HttpGet request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
HttpEntity responseEntity = httpClient.execute(request).getEntity();
String responseString = EntityUtils.toString(responseEntity);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(responseString);
int id = node.get("id").asInt();
String title = node.get("title").asText();
boolean completed = node.get("completed").asBoolean();
System.out.println("id: " + id);
System.out.println("title: " + title);
System.out.println("completed: " + completed);
}
}
在这个示例中,我们使用自定义的 HttpClient
,将其SSL使用的证书设置为信任所有证书,从而允许访问HTTPS站点。通过以上示例代码,可以方便地使用Java获取http和https协议返回的JSON数据。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java获取http和https协议返回的json数据 - Python技术站