`
michael1990
  • 浏览: 14045 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

android客户端序列化对象提交,服务器反序列化时出现ClassNotFoundException

 
阅读更多

 

android客户端序列化对象提交,服务器反序列化时出现ClassNotFoundException
解决:在服务器端也要有相同的序列化类Person,同时包名也要一样.
/**
     * @param serStr
     * @throws UnsupportedEncodingException
     * @throws IOException
     * @throws ClassNotFoundException
     * @描述 —— 将字符串反序列化成对象
     */
    public static Object getObjFromStr(String serStr)
              throws UnsupportedEncodingException, IOException,
             ClassNotFoundException {
         String redStr = java.net.URLDecoder.decode(serStr, "UTF-8");
         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                 redStr.getBytes( "ISO-8859-1" ));
         ObjectInputStream objectInputStream = new ObjectInputStream(
                 byteArrayInputStream);
         Object result = objectInputStream.readObject();
         objectInputStream.close();
         byteArrayInputStream. close();
 
          return result;
    }
 
    /**
     * @return
     * @throws IOException
     * @throws UnsupportedEncodingException
     * @描述 —— 将对象序列化成字符串
     */
    public static String getStrFromObj(Object obj) throws IOException,
             UnsupportedEncodingException {
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                 byteArrayOutputStream);
         objectOutputStream.writeObject(obj);
         String serStr = byteArrayOutputStream.toString("ISO-8859-1" );
         serStr = java.net.URLEncoder. encode(serStr, "UTF-8" );
 
         objectOutputStream.close();
         byteArrayOutputStream.close();
          return serStr;
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics