接收 InputStream 并从 Jersey 中的 json 字符串中获取值
Receive the InputStream and get the values from json string in Jersey
如何从 android 中的 HttpURlConenction
接收到来自 InputStream
的 JSON 字符串,以及如何获取它的值将它们存储到数据库 table?
{
"latitude":93.86898451,
"longitude":30.66561387,
"time":"24.04.2015 11:11:05",
"route":4
}
球衣接球手class:
@Path("data")
public class Receiver {
@POST
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
public void storeDate() {
BufferedReader in
= new BufferedReader(new InputStreamReader(process.getInputStream()));
}
}
使用 Jersey,您应该能够声明 class 数据(使用 Jackson 注释支持从 JSON 反序列化):
public class Data {
private double latitude;
private double longitude;
private String time;
private int route;
@JsonCreator
public Data(@JsonProperty("latitude") double latitude,
@JsonProperty("longitude") double longitude,
@JsonProperty("time") String time,
@JsonProperty("route") int route) {
this.latitude = latitude;
this.longitude = longitude;
this.time = time;
this.route = route;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public String getTime() {
return time;
}
public int getRoute() {
return route;
}
}
然后将您的端点定义为:
@Path("/data")
public class Receiver {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response storeData(Data data) {
// do something with data
return Response.status(201).build();
}
}
它应该可以正常工作。
1) 如果您可以修改客户端代码,请在您的 HttpURLConnection 连接中添加此行
connection.setRequestProperty("Content-Type", "application/json");
这会将输入转换为 POJO
2)如果您无法编辑客户端 HttpUrlConnection,则将流引入为
@Consumes("{*/*}")
public Something (String request){
ObjectMapper mapper = new ObjectMapper();
Data data = mapper.readValue(request, Data.class);
return = getSomething(data);
}
如何从 android 中的 HttpURlConenction
接收到来自 InputStream
的 JSON 字符串,以及如何获取它的值将它们存储到数据库 table?
{
"latitude":93.86898451,
"longitude":30.66561387,
"time":"24.04.2015 11:11:05",
"route":4
}
球衣接球手class:
@Path("data")
public class Receiver {
@POST
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
public void storeDate() {
BufferedReader in
= new BufferedReader(new InputStreamReader(process.getInputStream()));
}
}
使用 Jersey,您应该能够声明 class 数据(使用 Jackson 注释支持从 JSON 反序列化):
public class Data {
private double latitude;
private double longitude;
private String time;
private int route;
@JsonCreator
public Data(@JsonProperty("latitude") double latitude,
@JsonProperty("longitude") double longitude,
@JsonProperty("time") String time,
@JsonProperty("route") int route) {
this.latitude = latitude;
this.longitude = longitude;
this.time = time;
this.route = route;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public String getTime() {
return time;
}
public int getRoute() {
return route;
}
}
然后将您的端点定义为:
@Path("/data")
public class Receiver {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response storeData(Data data) {
// do something with data
return Response.status(201).build();
}
}
它应该可以正常工作。
1) 如果您可以修改客户端代码,请在您的 HttpURLConnection 连接中添加此行
connection.setRequestProperty("Content-Type", "application/json");
这会将输入转换为 POJO
2)如果您无法编辑客户端 HttpUrlConnection,则将流引入为
@Consumes("{*/*}")
public Something (String request){
ObjectMapper mapper = new ObjectMapper();
Data data = mapper.readValue(request, Data.class);
return = getSomething(data);
}