首先我们来详细讲解“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数据的方法示例 - Python技术站