ITeye博客

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

michael1990 · 2014-09-01 08:20 · Android · 编程语言 · Markdown

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;

    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。