首先我们来详细讲解“Java实现后台发送及接收json数据的方法示例”的攻略。在实现后台发送及接收json数据的过程中,可以使用Java中的两种方式:使用HttpURLConnection实现 JsonRequest 和使用HttpClient实现 Json 请求。下面分别来介绍这两种方式的具体实
首先我们来详细讲解“Java实现后台发送及接收json数据的方法示例”的攻略。在实现后台发送及接收json数据的过程中,可以使用Java中的两种方式:使用HttpURLConnection实现 JsonRequest 和使用HttpClient实现 Json 请求。下面分别来介绍这两种方式的具体实现。
使用HttpURLConnection实现JsonRequest
使用HttpURLConnection实现 JsonRequest,需要先在pom.xml文件中导入以下依赖:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
导入依赖后,在Java代码中,需要进行如下操作:
- 导入需要的类:
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
- 构造json数据:
String json = "{\"name\":\"张三\",\"age\":\"18\"}";
- 发送json数据:
private void sendJsonRequest() {
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url("http://www.example.com/postJson")
.post(requestBody)
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("发送json数据成功");
} else {
System.out.println("发送json数据失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 接收json数据:
private String getJsonResponse() {
String result = "";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.example.com/getJson")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
result = response.body().string();
System.out.println("接收json数据成功");
} else {
System.out.println("接收json数据失败");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
使用HttpClient实现Json请求
使用HttpClient实现Json请求,需要在pom.xml文件中导入以下依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
导入依赖后,在Java代码中,需要进行如下操作:
- 导入需要的类:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
- 构造json数据:
String json = "{\"name\":\"张三\",\"age\":\"18\"}";
- 发送json数据:
private void sendJsonRequest() {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.example.com/postJson");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(json, "UTF-8"));
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("发送json数据成功");
} else {
System.out.println("发送json数据失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 接收json数据:
private String getJsonResponse() {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.example.com/postJson");
httpPost.setHeader("Content-Type", "application/json");
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("接收json数据成功");
return result;
} else {
System.out.println("接收json数据失败");
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
以上就是使用HttpURLConnection或者使用HttpClient实现后台发送及接收json数据的方法示例,希望可以帮助你。
本文标题为:Java实现后台发送及接收json数据的方法示例
- SpringBoot从配置文件中获取属性的四种方法总结 2022-11-05
- PHP 巧用数组降低程序的时间复杂度 2024-02-02
- Java如何接收JSON数据 2023-10-08
- 如何在Java中判断一个字符串是否包含另一个字符串 2023-10-08
- Spring依赖注入的几种方式分享梳理总结 2023-02-27
- 详解Java中的final关键字 2023-06-11
- Java中excel表数据的批量导入方法 2024-01-29
- Springboot插件开发实战分享 2022-11-08
- MySQL MyBatis 默认插入当前时间方式 2023-06-10
- Java和SQL:返回null或抛出异常? 2023-11-01
