---
title: "android客户端序列化对象提交,服务器反序列化时出现ClassNotFoundException"
author: "michael1990"
date: "2014-09-01T08:20:05Z"
tags: []
categories: ["编程语言"]
slug: "android-classnotfoundexception"
draft: false
lastmod: "2014-09-01T08:20:05Z"
id: 2111196
url: "https://michael1990.iteye.com/blog/2111196"
---

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;

    }