求助:网络传输的序列化问题
客户端和服务器端同定义了一个User类,都实现了序列化接口,编号都是默认的1L.客户端使用HttpURLConnection,服务器端使用HttpServlet。
现在客户端欲向服务器端发送一个User的实体user,以POST的方式。代码如下:
URL url=new URL(UriPath);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
ObjectOutputStream out=new ObjectOutputStream(conn.getOutputStream());
out.writeObject(user);
out.flush();
out.close();
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
flag=in.read();
in.close();
conn.disconnect();
想法是:服务器端接收这个user,然后插入数据库。
但是不知什么原因,服务器端读取user时抛出异常,下面分别是服务器端的代码和异常信息
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
ObjectInputStream in=new ObjectInputStream(req.getInputStream());
try {
int t=in.readInt();
User user=(User)in.readObject();
System.out.print(t);
DatabaseTool dbTool=new DatabaseTool();
dbTool.addNewUser(user);
PrintWriter out=resp.getWriter();
out.write(1);
out.flush();
out.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
异常信息:
2012-10-21 18:48:14 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet AddNewUserServlet threw exception
java.lang.Error: Unresolved compilation problem:
t cannot be resolved
at com.quanquan.business.AddNewUserServlet.doPost(AddNewUserServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619) --------------------编程问答-------------------- 纠正一下,信息是这个:
java.lang.ClassNotFoundException: org.apache.harmony.luni.internal.util.ZoneInfo
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)
at java.util.Calendar.readObject(Calendar.java:2626)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.quanquan.business.AddNewUserServlet.doPost(AddNewUserServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619) --------------------编程问答-------------------- java.lang.ClassNotFoundException: org.apache.harmony.luni.internal.util.ZoneInfo
应该是缺少jar包吧 --------------------编程问答--------------------
不是 --------------------编程问答--------------------
好像真的是缺少了一个包 不过这到底是个什么东西
http://www.oschina.net/code/explore/android-2.2-froyo/org/apache/harmony/luni/internal/util/ZoneInfo.java --------------------编程问答--------------------
原来是我传输的user里面有一个日历的Calendar属性,注释掉,传输就不会报错了 到底是什么回事 --------------------编程问答-------------------- 贴出你的user类 看看
补充:移动开发 , Android