NoSuchAlgorithmException:ObjectInputStream.readObject()Android时找不到MessageDigest SHA实现

时间:2014-08-22 13:07:25

标签: java android exception

我将模型类保存在本地存储中以用于缓存目的

我使用以下命令将对象写入文件

private void writeData(Object data, String fileName) {
    try {
        FileOutputStream fos = context.openFileOutput(fileName,
            Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(data);
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

以下是从文件中读取对象的代码

public Object readData(String fileName) {
    Object data = null;
    if (context != null) {
        try {
            FileInputStream fis = context.openFileInput(fileName);
            ObjectInputStream is = new ObjectInputStream(fis);
            data = is.readObject();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    return data;
}

效果很好,但在某些设备中它会引发奇怪的异常,即

Caused by: java.security.NoSuchAlgorithmException: MessageDigest SHA implementation not found

我不知道后台发生了什么,readData如何抛出NoSuchAlogorithmException,我没有在这里使用任何MessageDigest。我在Java和Android文档中搜索了很多关于ObjectInputStream.readObject()的内容。我没有得到任何解决方案。

我试图为NoSuchAlgorithmException设置catch类,但在eclipse中将其显示为错误

Unreachable catch block for NoSuchAlgorithmException. This exception is never thrown from the try statement body

这里我附上完整的崩溃日志

java.lang.Error: java.security.NoSuchAlgorithmException: MessageDigest SHA implement    ation not found
at java.io.ObjectStreamClass.computeSerialVersionUID(ObjectStreamClass.java:420)
at java.io.ObjectStreamClass.cre    ateClassDesc(ObjectStreamClass.java:259)
at java.io.ObjectStreamClass.lookupStreamClass(ObjectStreamClass.java:1069)
at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2375)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1662)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1803)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at java.util.ArrayList.readObject(ArrayList.java:657)
at java.lang.reflect.Method.invokeN    ative(N    ative Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1354)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at com.test.util.CacheManager.readD    ata(CacheManager.java:104)
at com.test.SplashActivity.onCre    ate(SplashActivity.java:178)
at android.app.Activity.performCre    ate(Activity.java:5206)
at android.app.Instrument    ation.callActivityOnCre    ate(Instrument    ation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.disp    atchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeN    ative(N    ative Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.N    ativeStart.main(N    ative Method)
Caused by: java.security.NoSuchAlgorithmException: MessageDigest SHA implement    ation not found
at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
at java.security.MessageDigest.getInstance(MessageDigest.java:91)
at java.io.ObjectStreamClass.computeSerialVersionUID(ObjectStreamClass.java:418)
... 42 more
java.security.NoSuchAlgorithmException: MessageDigest SHA implement    ation not found
at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
at java.security.MessageDigest.getInstance(MessageDigest.java:91)
at java.io.ObjectStreamClass.computeSerialVersionUID(ObjectStreamClass.java:418)
at java.io.ObjectStreamClass.cre    ateClassDesc(ObjectStreamClass.java:259)
at java.io.ObjectStreamClass.lookupStreamClass(ObjectStreamClass.java:1069)
at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2375)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1662)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1803)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at java.util.ArrayList.readObject(ArrayList.java:657)
at java.lang.reflect.Method.invokeN    ative(N    ative Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1354)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at com.test.util.CacheManager.readD    ata(CacheManager.java:104)
at com.test.SplashActivity.onCre    ate(SplashActivity.java:178)
at android.app.Activity.performCre    ate(Activity.java:5206)
at android.app.Instrument    ation.callActivityOnCre    ate(Instrument    ation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.disp    atchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeN    ative(N    ative Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.N    ativeStart.main(N    ative Method)

修改

这是我尝试编写和阅读的课程。内部类我使用AutoPromoData,OrderBkgPixData SubscriptionUrlData等,这个类里面包含简单的int和string和url的字符串。

Configuration.jvaa

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;
import com.test.model.CacheTimeData;
import com.test.SplashUrlData;
import com.test.SubscriptionUrlData;

public class Configuration {

    int version;

    @SerializedName("pivotCodeRetour")
    int pivotCodeRetour;

    ArrayList<WSTimeOutData> WSTimeOut;
    ArrayList<AutoPromoData> AutoPromo;
    ArrayList<StreetMktData> StressMkt;
    ArrayList<OrderBkgPixData> OrderBkgPix;
    ArrayList<LogBkgPixData> LogBkgPix;
    ArrayList<SubscriptionUrlData> SubscriptionBkgPix;

    String Voucher;
    ArrayList<PhoneContactData> PhoneContact;
    String ProdAvailability;
    String PaymentUrl;
    String TravSaleHomeUrl;
    String TravCartUrl;
    String TravOrderDetailUrl;
    String TicketSaleHomeUrl;
    String TicketOrderUrl;
    String CGV;
    String wishlist;

    int pushCart;
    int pushVoucher;

    ArrayList<SplashUrlData> Splashscreen;
    ArrayList<CacheTimeData> CacheTime;

    public int getPushCart() {
        return pushCart;
    }

    public ArrayList<SubscriptionUrlData> getSubscriptionBkgPix() {
        return SubscriptionBkgPix;
    }

    public void setSubscriptionBkgPix(
            ArrayList<SubscriptionUrlData> subscriptionBkgPix) {
        SubscriptionBkgPix = subscriptionBkgPix;
    }

    public void setPushCart(int pushCart) {
        this.pushCart = pushCart;
    }

    public int getPushVoucher() {
        return pushVoucher;
    }

    public void setPushVoucher(int pushVoucher) {
        this.pushVoucher = pushVoucher;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public ArrayList<WSTimeOutData> getWSTimeOut() {
        return WSTimeOut;
    }

    public void setWSTimeOut(ArrayList<WSTimeOutData> wSTimeOut) {

        WSTimeOut = wSTimeOut;
    }

    public ArrayList<AutoPromoData> getAutoPromo() {
        return AutoPromo;
    }

    public void setAutoPromo(ArrayList<AutoPromoData> autoPromo) {
        AutoPromo = autoPromo;
    }

    public ArrayList<StreetMktData> getStressMkt() {
        return StressMkt;
    }

    public void setStressMkt(ArrayList<StreetMktData> stressMkt) {
        StressMkt = stressMkt;
    }

    public ArrayList<OrderBkgPixData> getOrderBkgPix() {
        return OrderBkgPix;
    }

    public void setOrderBkgPix(ArrayList<OrderBkgPixData> orderBkgPix) {
        OrderBkgPix = orderBkgPix;
    }

    public ArrayList<LogBkgPixData> getLogBkgPix() {
        return LogBkgPix;
    }

    public void setLogBkgPix(ArrayList<LogBkgPixData> logBkgPix) {
        LogBkgPix = logBkgPix;
    }

    public String getVoucher() {
        return Voucher;
    }

    public void setVoucher(String voucher) {
        Voucher = voucher;
    }

    public ArrayList<PhoneContactData> getPhoneContact() {
        return PhoneContact;
    }

    public void setPhoneContact(ArrayList<PhoneContactData> phoneContact) {
        PhoneContact = phoneContact;
    }

    public String getProdAvailability() {
        return ProdAvailability;
    }

    public void setProdAvailability(String prodAvailability) {
        ProdAvailability = prodAvailability;
    }

    public String getPaymentUrl() {
        return PaymentUrl;
    }

    public void setPaymentUrl(String paymentUrl) {
        PaymentUrl = paymentUrl;
    }

    public String getTravSaleHomeUrl() {
        return TravSaleHomeUrl;
    }

    public void setTravSaleHomeUrl(String travSaleHomeUrl) {
        TravSaleHomeUrl = travSaleHomeUrl;
    }

    public String getTravCartUrl() {
        return TravCartUrl;
    }

    public void setTravCartUrl(String travCartUrl) {
        TravCartUrl = travCartUrl;
    }

    public String getTravOrderDetailUrl() {
        return TravOrderDetailUrl;
    }

    public void setTravOrderDetailUrl(String travOrderDetailUrl) {
        TravOrderDetailUrl = travOrderDetailUrl;
    }

    public String getTicketSaleHomeUrl() {
        return TicketSaleHomeUrl;
    }

    public void setTicketSaleHomeUrl(String ticketSaleHomeUrl) {
        TicketSaleHomeUrl = ticketSaleHomeUrl;
    }

    public String getTicketOrderUrl() {
        return TicketOrderUrl;
    }

    public void setTicketOrderUrl(String ticketOrderUrl) {
        TicketOrderUrl = ticketOrderUrl;
    }

    public ArrayList<SplashUrlData> getSplashscreen() {
        return Splashscreen;
    }

    public void setSplashscreen(ArrayList<SplashUrlData> splashscreen) {
        Splashscreen = splashscreen;
    }

    public ArrayList<CacheTimeData> getCacheTime() {
        return CacheTime;
    }

    public void setCacheTime(ArrayList<CacheTimeData> cacheTime) {
        CacheTime = cacheTime;
    }

    public int getPivotCodeRetour() {
        return pivotCodeRetour;
    }

    public void setPivotCodeRetour(int pivotCodeRetour) {
        this.pivotCodeRetour = pivotCodeRetour;
    }

    public String getCGV() {
        return CGV;
    }

    public void setCGV(String cGV) {
        CGV = cGV;
    }

    public String getWishlist() {
        return wishlist;
    }

    public void setWishlist(String wishlist) {
        this.wishlist = wishlist;
    }
}

1 个答案:

答案 0 :(得分:1)

它是针对相关设备的错误报告,但您可以通过始终在Serializable类中定义自己的serialVersionUID来避免它,您应该出于其他原因这样做。当Java为这个缺失的类属性创建一个默认值时,就会发生这种情况。