protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("msg").toString());
String data = request.getParameter("msg").toString();
Gson gson = new Gson();
MessageBase msggg = gson.fromJson(data, MessageBase.class);
//System.out.println(msggg.Id + msggg.MessageText);
}
public abstract class MessageBase implements Serializable {
public int Id;
public String MessageText;
public Date ReceiveDate;
}
public class SyncSmsMessage extends MessageBase {
public String SenderNum;
}
该代码有效,直到MessageBase msggg=gson.fromJson(data, MessageBase.class);
. 我得到这个例外:
java.lang.RuntimeException: Failed to invoke public com.example.syncapp.MessageBase() with no args
at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:94)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at AndroidServlet.doPost(AndroidServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
我需要做什么?我将 .jar 放在 lib 文件夹中,我认为 tomcat 可以很好地加载 .jar。