Java 为什么我的服务器端方法不能使用android请求中的json对象,但另一个json对象可以?
我正在尝试向jersey编写的服务器发送一个json对象。邮递员给我做了测试。当我使用我想要发送的类时,它可以被服务器读取(空对象)。但当我用另一个对象替换要发送的对象时。这是工作。我还是不知道会发生什么 我尝试发送一个对象(FilmWatched.class),如下所示: { “id_电影”:“F04”, “cur_progress”:120, “用户名”:“tony123” } 但我的服务器端的方法只是从中读取一个空对象。当我用另一个对象(Profile.class)重新封装它并在方法params中修改一点来读取Profile对象及其工作时,服务器端的方法可以从json中读取所有信息。这是: { “用户名”:“tony123”, “fistName”:“Tony”, “姓氏”:“斯塔克”, “图像”:“tony123.png” } 电影节Java 为什么我的服务器端方法不能使用android请求中的json对象,但另一个json对象可以?,java,android,jersey,gson,Java,Android,Jersey,Gson,我正在尝试向jersey编写的服务器发送一个json对象。邮递员给我做了测试。当我使用我想要发送的类时,它可以被服务器读取(空对象)。但当我用另一个对象替换要发送的对象时。这是工作。我还是不知道会发生什么 我尝试发送一个对象(FilmWatched.class),如下所示: { “id_电影”:“F04”, “cur_progress”:120, “用户名”:“tony123” } 但我的服务器端的方法只是从中读取一个空对象。当我用另一个对象(Profile.class)重新封装它并在方法par
public class FilmWatched {
@SerializedName("id_film")
@Expose
private String id_film;
@SerializedName("cur_progress")
@Expose
private int cur_progress;
@SerializedName("username_")
@Expose
private String username_;
public FilmWatched() {
super();
}
public FilmWatched(String idFilm, int curProgress, String username) {
super();
this.id_film = idFilm;
this.cur_progress = curProgress;
this.username_ = username;
}
//....getter and setter....
}
Profile.class
public class Profile {
@SerializedName("username")
@Expose
private String username;
@SerializedName("fistName")
@Expose
private String fistName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("image")
@Expose
private String image;
public Profile() {
super();
}
public Profile(String username, String fistName, String lastName,
String image) {
this.username = username;
this.fistName = fistName;
this.lastName = lastName;
this.image = image;
}
public String getUsername() {
return username;
}
//....getter and setter....
}
这是我在服务器端的方法:
@Path("/Film")
public class FilmService {
@PUT
@Path("Update/WatchedFilm/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response updateFilmWatched(FilmWatched filmWatched)
{
boolean response= false;
//update data when it was read
DBConnector db = new DBConnector();
boolean isUpdated = db.updateFilmWatched(filmWatched);
//log some thing in consolse and response to client
String s = isUpdated ? "Time progress updated" : "Cannot update Time progress";
response = isUpdated ? true : false;
System.out.println(s);
return Response.
status(Response.Status.OK).
entity(new Gson().toJson(response)).
build();
}
}
我希望服务器端的方法可以读取对象FilmWatched的json。我解决了这个问题。通过改变服务器端“updateFilmWatched”方法中的参数。电影观看->字符串。。。然后我将其转换为json。