在android中接收序列化的arraylist

时间:2014-05-02 14:46:50

标签: java android serialization arraylist

我正在尝试获取包含来自java服务器的对象的序列化ArrayList,但是当我在android应用程序中进行反序列化时会发生错误。

所以我有:

  • ArrayList
  • 每个投票都包含 ArrayList
  • 每个选项包含 EncryptedInteger 对象

投票类

    package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Vote implements Serializable {

    private static final long serialVersionUID = 2L;
    private int voteId;
    private int surveyId;
    private int voterId;
    private List<Choice> choices;
    private boolean counted;


    public Vote(){

    }

    public Vote(int voteId,int surveyId,int voterId, boolean counted){
        this.voteId=voteId;
        this.surveyId=surveyId;
        this.voterId=voterId;
        this.choices = new ArrayList<Choice>();
        this.counted=counted;
    }

    public void addChoice(Choice choice) {
        choices.add(choice);
    }

    public int getVoteId() {
        return voteId;
    }

    public void setVoteId(int voteId) {
        this.voteId = voteId;
    }

    public int getSurveyId() {
        return surveyId;
    }

    public void setSurveyId(int surveyId) {
        this.surveyId = surveyId;
    }

    public int getVoterId() {
        return voterId;
    }

    public void setVoterId(int voterId) {
        this.voterId = voterId;
    }

    public List<Choice> getChoices() {
        return choices;
    }

    public void setChoices(List<Choice> choices) {
        this.choices = choices;
    }

    public boolean isCounted() {
        return counted;
    }

    public void setCounted(boolean counted) {
        this.counted = counted;
    }


}

AND此对象包含选项的arraylist

选择课程

    package entities;

import java.io.Serializable;

import thep.paillier.EncryptedInteger;

public class Choice implements Serializable  {
private static final long serialVersionUID = 2L;
  private EncryptedInteger encryptedInteger;
  private int questionNumber;
  private int optionNumber;
  private double S;


public Choice(EncryptedInteger encryptedInteger, int questionNumber,
        int optionNumber, double s) {

    this.encryptedInteger = encryptedInteger;
    this.questionNumber = questionNumber;
    this.optionNumber = optionNumber;
    S = s;
}
public EncryptedInteger getEncryptedInteger() {
    return encryptedInteger;
}
public void setEncryptedInteger(EncryptedInteger encryptedInteger) {
    this.encryptedInteger = encryptedInteger;
}
public int getQuestionNumber() {
    return questionNumber;
}
public void setQuestionNumber(int questionNumber) {
    this.questionNumber = questionNumber;
}
public int getOptionNumber() {
    return optionNumber;
}
public void setOptionNumber(int optionNumber) {
    this.optionNumber = optionNumber;
}
public double getS() {
    return S;
}
public void setS(double s) {
    S = s;
}

}

将创建arraylist的代码

@Path("/vote")
public class GenerateNumberRequest {

    @POST
    @Path("/GNA")
    @Consumes("application/x-www-form-urlencoded")
    public Response GNApost(MultivaluedMap<String, String> form,
            @Context HttpServletResponse httpResponse) {
        int voteId = Integer.parseInt(form.getFirst("voteId"));
        String generateNumberAuthenticator = form
                .getFirst("generateNumberAuthenticator");

        SurveyKey key = null;
        VoteAuthenticator authenticator = null;
        VoterVerifierRepository voterVerifierRepository = new VoterVerifierRepository();
        VoteAuthenticatorRepository voteAuthenticatorRepository = new VoteAuthenticatorRepository();
        SurveyKeyRepository surveyKeyRepository = new SurveyKeyRepository();
        try {
            authenticator = voteAuthenticatorRepository
                    .getVoteAuthenticator(voteId);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int surveyId = authenticator.getSurveyId();
        SurveyCommunication surveyCommunication = new SurveyCommunication(
                surveyId);

        Survey survey = surveyCommunication.getSurvey();
        try {
            key = surveyKeyRepository.getSurveyKey(surveyId);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        List<Vote> votes = new ArrayList<Vote>();

        Vote voteZero = new Vote(authenticator.getVoteId(),
                authenticator.getSurveyId(), authenticator.getVoterId(), false);
        Vote voteOne = new Vote(authenticator.getVoteId(),
                authenticator.getSurveyId(), authenticator.getVoterId(), false);
        Random random = new Random();
        double s1, s2, s3;
        PublicKey publicKey = key.getPublicKey();
        EncryptedInteger encryptedZero = null;
        EncryptedInteger encryptedOne = null;
        for (Question q : survey.getAllQuestions()) {
            for (Option o : q.getOptions()) {
                s1 = random.nextInt() % 10000;
                s2 = -(s1 * key.getX()) / key.getY();
                s3 = (1 - (s1 * key.getX())) / key.getY();

                try {
                    encryptedZero = new EncryptedInteger(BigInteger.ZERO,
                            publicKey);
                    encryptedOne = new EncryptedInteger(BigInteger.ONE,
                            publicKey);
                } catch (BigIntegerClassNotValid e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                voteZero.addChoice(new Choice(encryptedZero, q.getqID(), o
                        .getOptionNumaricalValue(), s2));
                voteOne.addChoice(new Choice(encryptedOne, q.getqID(), o
                        .getOptionNumaricalValue(), s3));
            }
        }

        votes.add(voteZero);
        votes.add(voteOne);

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o;
        try {
            o = new ObjectOutputStream(b);
            o.writeObject(votes);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Response.status(200).entity(b.toByteArray()).build();

    }
}

安卓代码

    package com.example.connection;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import entities.Vote;

public class GenerateNumberConnection {

    static HttpClient httpClient;
    static HttpPost httpPost;
    static HttpResponse response;

    @SuppressWarnings("unchecked")
    public static List<Vote> getVotes(String voteId, String GNA) {
        httpClient = HttpObjects.getHttpClient();
        httpPost = new HttpPost(
                "http://192.168.100.4:8181/trustee/APP/vote/GNA");

        List<Vote> votes = null;
        ArrayList<NameValuePair> postParameters;

        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("voteId", voteId));
        postParameters.add(new BasicNameValuePair(
                "generateNumberAuthenticator", GNA));

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            byte[] responseByte = null;
            responseByte = EntityUtils.toByteArray(entity);
            ByteArrayInputStream in = new ByteArrayInputStream(responseByte);
            ObjectInputStream is = new ObjectInputStream(in);
            votes = (List<Vote>) is.readObject();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return votes;
    }
}

代码的痕迹

05-02 09:58:50.248: W/System.err(934): java.lang.ClassNotFoundException: sun.security.provider.Sun
05-02 09:58:50.248: W/System.err(934):  at java.lang.Class.classForName(Native Method)
05-02 09:58:50.248: W/System.err(934):  at java.lang.Class.forName(Class.java:251)
05-02 09:58:50.248: W/System.err(934):  at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2262)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1638)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:658)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1781)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
05-02 09:58:50.268: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.278: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.278: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.278: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.278: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.278: W/System.err(934):  at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
05-02 09:58:50.288: W/System.err(934):  at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
05-02 09:58:50.288: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
05-02 09:58:50.288: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.298: W/System.err(934):  at java.util.ArrayList.readObject(ArrayList.java:661)
05-02 09:58:50.298: W/System.err(934):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:58:50.298: W/System.err(934):  at java.lang.reflect.Method.invoke(Method.java:515)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.298: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.308: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.308: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.318: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.318: W/System.err(934):  at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
05-02 09:58:50.318: W/System.err(934):  at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
05-02 09:58:50.318: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
05-02 09:58:50.318: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.328: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.328: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.348: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.348: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.348: W/System.err(934):  at java.util.ArrayList.readObject(ArrayList.java:661)
05-02 09:58:50.348: W/System.err(934):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:58:50.348: W/System.err(934):  at java.lang.reflect.Method.invoke(Method.java:515)
05-02 09:58:50.348: W/System.err(934):  at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
05-02 09:58:50.348: W/System.err(934):  at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
05-02 09:58:50.358: W/System.err(934):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
05-02 09:58:50.358: W/System.err(934):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
05-02 09:58:50.358: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
05-02 09:58:50.358: W/System.err(934):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
05-02 09:58:50.358: W/System.err(934):  at com.example.connection.GenerateNumberConnection.getVotes(GenerateNumberConnection.java:57)
05-02 09:58:50.358: W/System.err(934):  at com.example.elections.PagesValue.readWebServices(PagesValue.java:396)
05-02 09:58:50.358: W/System.err(934):  at com.example.elections.PagesValue$8.onClick(PagesValue.java:265)
05-02 09:58:50.358: W/System.err(934):  at android.view.View.performClick(View.java:4438)
05-02 09:58:50.358: W/System.err(934):  at android.view.View$PerformClick.run(View.java:18422)
05-02 09:58:50.358: W/System.err(934):  at android.os.Handler.handleCallback(Handler.java:733)
05-02 09:58:50.358: W/System.err(934):  at android.os.Handler.dispatchMessage(Handler.java:95)
05-02 09:58:50.358: W/System.err(934):  at android.os.Looper.loop(Looper.java:136)
05-02 09:58:50.358: W/System.err(934):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 09:58:50.358: W/System.err(934):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:58:50.358: W/System.err(934):  at java.lang.reflect.Method.invoke(Method.java:515)
05-02 09:58:50.358: W/System.err(934):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 09:58:50.358: W/System.err(934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 09:58:50.368: W/System.err(934):  at dalvik.system.NativeStart.main(Native Method)
05-02 09:58:50.368: W/System.err(934): Caused by: java.lang.NoClassDefFoundError: sun/security/provider/Sun
05-02 09:58:50.368: W/System.err(934):  ... 73 more
05-02 09:58:50.368: W/System.err(934): Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.security.provider.Sun" on path: DexPathList[[zip file "/data/app/com.example.elections-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.elections-1, /system/lib]]
05-02 09:58:50.388: W/System.err(934):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-02 09:58:50.388: W/System.err(934):  at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-02 09:58:50.388: W/System.err(934):  at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-02 09:58:50.388: W/System.err(934):  ... 73 more
05-02 09:58:50.758: D/AndroidRuntime(934): Shutting down VM
05-02 09:58:50.758: W/dalvikvm(934): threadid=1: thread exiting with uncaught exception (group=0xb1a39ba8)
05-02 09:58:50.808: E/AndroidRuntime(934): FATAL EXCEPTION: main
05-02 09:58:50.808: E/AndroidRuntime(934): Process: com.example.elections, PID: 934
05-02 09:58:50.808: E/AndroidRuntime(934): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.elections/com.example.elections.HomePage}: java.lang.NullPointerException
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.os.Looper.loop(Looper.java:136)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 09:58:50.808: E/AndroidRuntime(934):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:58:50.808: E/AndroidRuntime(934):  at java.lang.reflect.Method.invoke(Method.java:515)
05-02 09:58:50.808: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 09:58:50.808: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 09:58:50.808: E/AndroidRuntime(934):  at dalvik.system.NativeStart.main(Native Method)
05-02 09:58:50.808: E/AndroidRuntime(934): Caused by: java.lang.NullPointerException
05-02 09:58:50.808: E/AndroidRuntime(934):  at com.example.elections.HomePage.onCreate(HomePage.java:53)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.Activity.performCreate(Activity.java:5231)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 09:58:50.808: E/AndroidRuntime(934):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 09:58:50.808: E/AndroidRuntime(934):  ... 11 more
05-02 09:58:55.218: D/gralloc_goldfish(1074): Emulator without GPU emulation detected.
05-02 10:00:31.428: D/dalvikvm(1074): GC_FOR_ALLOC freed 139K, 6% free 3246K/3452K, paused 30ms, total 37ms

我花了很多时间搜索,但我找不到任何东西。

编辑一个:

EncryptedInteger类here

1 个答案:

答案 0 :(得分:1)

我猜测EncryptedInteger实例指向sun.security.provider.Sun的实例。由于sun.security.provider.Sun不在您的Android应用程序的类路径中,因此它不知道如何对其进行反序列化并将其炸毁。

我认为它是SecureRandom中的EncryptedInteger