簡體   English   中英

Java-為什么我的JSON方法出現stackoverflow錯誤?

[英]Java - Why stackoverflow error in my JSON method?

我需要您的幫助以了解為什么在我的方法中對象分配會返回堆棧溢出錯誤。

方法如下:

public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }

這里是該類的所有代碼:

package com.neocloud.model;

import java.io.Serializable;

import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.dao.GroupDAO;
import com.neocloud.model.dao.SubscriptionDAO;

public class Subscription implements Serializable{

    private static final long serialVersionUID = -3901714994276495605L;
    private long id;
    private Device dispositivo;
    private Group  grupo;
    private String subscription;


    public Subscription(){

        clear();

    }

    public void clear(){

        id = 0;
        subscription = "";
        grupo = new Group();
        dispositivo = new Device();

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }


    public String registerDB(boolean registrarSNS){

        GroupDAO gDAO = new GroupDAO();
        DeviceDAO dDAO = new DeviceDAO();

        gDAO.selectById(this.grupo);
        dDAO.selectById(this.dispositivo);

        if (registrarSNS)
            registerSNS(this.dispositivo.getEndPointArn(), this.grupo.getTopicArn());

        SubscriptionDAO sDAO = new SubscriptionDAO();
        return sDAO.insert(this);
    }

    public String registerSNS(String endpointArn, String topicArn){

        String resposta;
        AwsSns sns = AwsSns.getInstance();
        resposta = ""+sns.addSubscriptionToTopic(endpointArn, topicArn);
        setSubscription(resposta);

        return resposta;        
    }

    public String delete(){

        AwsSns sns = AwsSns.getInstance();
        SubscriptionDAO sDAO = new SubscriptionDAO();
        sDAO.selectById(this);

        sns.deleteSubscriptionFromTopic(this.subscription);

        return sDAO.delete(this);

    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public String getSubscription() {
        return subscription;
    }

    public void setSubscription(String subscription) {
        if (subscription != null){
            if (!this.subscription.equals(subscription))
                this.subscription = subscription;
        }else
            this.subscription = new String();
    }

    public Device getDispositivo() {
        return dispositivo;
    }

    public void setDispositivo(Device dispositivo) {
        if (dispositivo != null){
            if (dispositivo.getId() != this.dispositivo.getId())
                this.dispositivo = dispositivo;
        }else
            this.dispositivo = new Device();
    }

    public Group getGrupo() {
        return grupo;
    }

    public void setGrupo(Group grupo) {
        if (grupo != null){
            if (grupo.getId() != this.grupo.getId())
                this.grupo = grupo;
        }else
            this.grupo = new Group();
    }

}

這是Device類:

package com.neocloud.model;

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

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.enums.EEnvironment;
import com.neocloud.model.enums.EProduct;
import com.neocloud.model.enums.ESystem;

public class Device implements Serializable{


    private static final long serialVersionUID = 8515474788917476721L;
    private long id;
    private String development;
    private String SSL = "tls://gateway.push.apple.com:2195";
    private String feedback = "tls://feedback.push.apple.com:2196";
    private String sandboxSSL = "tls://gateway.sandbox.push.apple.com:2195";
    private String sandboxFeedback = "tls://feedback.sandbox.push.apple.com:2196";
    private String message;
    private String endPointArn;
    private String deviceToken;
    private EProduct appID;
    private String appVersion;
    private String deviceUID;
    private String deviceName;
    private String deviceModel;
    private String deviceVersion;
    private boolean pushBadge;
    private boolean pushAlert;
    private boolean pushSound;
    private ESystem system;
    private EEnvironment environment;
    private ArrayList<Subscription> subscriptions;
    private String deviceUser;


    public Device(){

        clear();

    }

    public String deleteToken(){
        /*
         * 
         * terminar aqui ainda...
         * 
        DeviceDAO dDAO = new DeviceDAO();

        AwsSns sns = AwsSns.getInstance();

        sns.deleteDeviceEndpoint(getEndPointArn());

        ArrayList<String> subscriptions = dDAO.getSubscriptionsFromEndpointArn(this);

        for (int i=0; i<subscriptions.size();i++){

            sns.deleteSubscriptionFromTopic(subscriptions.get(i));

        }

        return dDAO.delete(this);
        */ return "";
    }

    public String updateToken(){

        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = ""+sns.updateDeviceEndpoint(getEndPointArn(), deviceToken);

        DeviceDAO dDAO = new DeviceDAO();
        dDAO.update(this);

        return jsonResposta;

    }

    public String registerDevice(){

        if (appVersion.length() == 0)
            return "applen0";
        if (deviceUID.length() > 40)
            return "deviceUID40";
        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";
        if (deviceName.length() == 0)
            return "deviceNamelen0";
        if (deviceModel.length() == 0)
            return "deviceModellen0";
        if (deviceVersion.length() == 0)
            return "deviceVersionlen0";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = null;

        switch (getAppID()) {
            case MODULE: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEANDROID);
                break;

            }
            case HOSTPRO: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROANDROID);                  
                break;

            }
            case CONNEXOON: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONANDROID);
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }


        if (jsonResposta != null){

            JSONObject json;
            try {
                jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
                jsonResposta = jsonResposta.replace("{EndPointArn:", "{\"EndPointArn\":");
                json = new JSONObject(jsonResposta);
                setEndPointArn(json.getString("EndpointArn"));
            } catch (JSONException e) {

                e.printStackTrace();
            }

        }

        Subscription subscription;

        switch (getAppID()){
            case MODULE: {

                subscription = new Subscription();
                subscription.getGrupo().setId(19);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEGERAL);
                subscriptions.add(subscription);


                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(25);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(22);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case HOSTPRO: {

                subscription = new Subscription();
                subscription.getGrupo().setId(10);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(16);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(13);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROANDROID);
                    subscriptions.add(subscription);

                }   
                break;

            }
            case CONNEXOON: {

                subscription = new Subscription();
                subscription.getGrupo().setId(1);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(7);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(4);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }

        DeviceDAO dDAO = new DeviceDAO();
        return dDAO.insert(this);

    }


    public void clear(){

        development = "";
        message = "";
        endPointArn = "";
        deviceToken = "";
        appID = EProduct.NONE;
        appVersion = "";
        deviceUID = "";
        deviceName = "";
        deviceModel = "";
        deviceVersion = "";
        pushBadge = false;
        pushAlert = false;
        pushSound = false;
        system = ESystem.NONE;
        environment = EEnvironment.HOMOLOGATION;
        subscriptions = new ArrayList<Subscription>();
        deviceUser = "";

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }


    public String getDevelopment() {
        return development;
    }

    public void setDevelopment(String development) {
        if (development != null){
            if (!development.equals(this.development))
                this.development = development;
        }
        this.development = new String();
    }

    public String getSSL() {
        return SSL;
    }

    public void setSSL(String sSL) {
        if (sSL != null){
            if (!sSL.equals(this.SSL))
                this.SSL = sSL;
        }
        else
            this.SSL = new String();
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        if (feedback != null){
            if (!feedback.equals(this.feedback))
                this.feedback = feedback;
        }
        this.feedback = new String();
    }

    public String getSandboxSSL() {
        return sandboxSSL;
    }

    public void setSandboxSSL(String sandboxSSL) {

        if (sandboxSSL != null){
            if (!sandboxSSL.equals(this.sandboxSSL))
                this.sandboxSSL = sandboxSSL;
        }else       
            this.sandboxSSL = new String();
    }

    public String getSandboxFeedback() {
        return sandboxFeedback;
    }

    public void setSandboxFeedback(String sandboxFeedback) {
        if (sandboxFeedback != null){
            if (!sandboxFeedback.equals(this.sandboxFeedback))
                this.sandboxFeedback = sandboxFeedback;
        }else
            this.sandboxFeedback = new String();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (message != null){
            if (!message.equals(this.message))
                this.message = message;
        }else
            this.message = new String();
    }

    public String getEndPointArn() {
        return endPointArn;
    }

    public void setEndPointArn(String createPlatformEndpointResult) {
        if (createPlatformEndpointResult != null){
            if (!createPlatformEndpointResult.equals(this.endPointArn))
                this.endPointArn = createPlatformEndpointResult;
        }else
            this.endPointArn = new String();
    }

    public String getDeviceToken() {
        return deviceToken;
    }

    public void setDeviceToken(String deviceToken) {
        if (deviceToken != null){
            if (!deviceToken.equals(this.deviceToken))
                this.deviceToken = deviceToken;
        }else
            this.deviceToken = new String();
    }

    public EProduct getAppID() {
        return appID ;
    }

    public void setAppID(EProduct appID){
        if (appID != this.appID)
            this.appID = appID;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        if (appVersion != null){
            if (!appVersion.equals(this.appVersion))
                this.appVersion = appVersion;
        }else
            this.appVersion = new String();
    }

    public String getDeviceUID() {
        return deviceUID;
    }

    public void setDeviceUID(String deviceUID) {
        if (deviceUID != null){
            if (!deviceUID.equals(this.deviceUID))
                this.deviceUID = deviceUID;
        }else
            this.deviceUID = new String();
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        if (deviceName != null){
            if (!deviceName.equals(this.deviceName))
                this.deviceName = deviceName;
        }else
            this.deviceName = new String();
    }

    public String getDeviceModel() {
        return deviceModel;
    }

    public void setDeviceModel(String deviceModel) {
        if (deviceModel != null){
            if (!deviceModel.equals(this.deviceModel))
                this.deviceModel = deviceModel;
        }else
            this.deviceModel = new String();
    }

    public String getDeviceVersion() {
        return deviceVersion;
    }

    public void setDeviceVersion(String deviceVersion) {
        if (deviceVersion != null){
            if (!deviceVersion.equals(this.deviceVersion))
                this.deviceVersion = deviceVersion;
        }else
            this.deviceVersion = new String();
    }

    public boolean isPushBadge() {
        return pushBadge;
    }

    public void setPushBadge(boolean pushBadge) {
        if (pushBadge != this.pushBadge)
            this.pushBadge = pushBadge;
    }

    public boolean isPushAlert() {
        return pushAlert;
    }

    public void setPushAlert(boolean pushAlert) {
        if (pushAlert != this.pushAlert)
            this.pushAlert = pushAlert;
    }

    public boolean isPushSound() {
        return pushSound;
    }

    public void setPushSound(boolean pushSound) {
        if (this.pushSound != pushSound)
            this.pushSound = pushSound;
    }

    public ESystem getSystem() {
        return system;
    }

    public void setTipoSmartphone(ESystem system) {
        if (system != this.system)
            this.system = system;
    }

    public EEnvironment getEnvironment() {
        return environment;
    }

    public void setEnvironment(EEnvironment environment) {
        if (environment != this.environment)
            this.environment = environment;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public void setSystem(ESystem system) {
        if (system != this.system)
    this.system = system;
    }

    public ArrayList<Subscription> getSubscriptions() {
        return subscriptions;
    }

    public void setSubscriptions(ArrayList<Subscription> subscriptions) {
        if (subscriptions != null)
            this.subscriptions = subscriptions;
        else
            this.subscriptions = new ArrayList<Subscription>();
    }

    public String getDeviceUser() {
        return deviceUser;
    }

    public void setDeviceUser(String deviceUser) {
        if (deviceUser != null){
            if (!deviceUser.equals(this.deviceUser))
                this.deviceUser = deviceUser;
        }else
            this.deviceUser = new String();
    }



}

這是Group類:

package com.neocloud.model;

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

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.GroupDAO;

public class Group implements Serializable{


    private static final long serialVersionUID = -5032857327241801763L;
    private long id;
    private String nome;
    private String topicArn;

    public Group(){
        this.clear();
    }

    public void clear(){
        this.id = 0;
        this.nome = new String();
        this.topicArn = new String();
    }

    public String registerGrupo(){

        AwsSns snsClient = AwsSns.getInstance();

        String topic = ""+snsClient.createTopic(this.nome);

        JSONObject json;
        String jsonResposta = topic;
        try {
            jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
            jsonResposta = jsonResposta.replace("{TopicArn:", "{\"TopicArn\":");
            json = new JSONObject(jsonResposta);
            setTopicArn(json.getString("TopicArn"));
        } catch (JSONException e) {

            e.printStackTrace();
        }

        GroupDAO grupoDAO = new GroupDAO();
        return grupoDAO.insert(this);

    }

    public String deleteGrupo(){
        AwsSns snsClient = AwsSns.getInstance();

        GroupDAO grupoDAO = new GroupDAO();
        ArrayList<Group> grupos = grupoDAO.select(this);

        if (grupos.size() > 0){

            for (int i = 0; i<grupos.size(); i++){
                this.topicArn = grupos.get(i).getTopicArn();
                this.nome = grupos.get(i).getNome();
                this.id = grupos.get(i).getId();
                snsClient.deleteTopic(this.topicArn);

            }

            return grupoDAO.delete(this);

        }

        return "-1";
    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTopicArn() {
        return topicArn;
    }
    public void setTopicArn(String topicArn) {
        this.topicArn = topicArn;
    }

}

這是Tomcat日志:

線程“ http-bio-8080-exec-5”中的異常,java.lang.reflect.Executable。處的java.lang.StackOverflowError。(java.lang.reflect.Method。處的未知源)。 java.lang.reflect.ReflectAccess.copyMethod(Unknown Source)處的java.lang.reflect.ReflectionFactory.copyMethod(Unknown Source)處java.lang.Class.copyMethods(Unknown Source)處的Reflection.Method.copy(Unknown Source)。 com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:930)的lang.Class.getMethods(未知源)com.amazonaws.util.json.JSONObject。(JSONObject.java:285)的com.amazonaws .util.json.JSONObject.wrap(JSONObject.java:1540)位於com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:960)

感謝所有幫助!

也許是對象中的循環引用。

json = new JSONObject(this);

您正在嘗試對此進行JSON化

id = 0;
subscription = "";
grupo = new Group();
dispositivo = new Device();

而且我認為以某種方式,它無法JSONify GroupDevice 您能告訴我們這些類的代碼嗎?

更新:在您的Subscription.clear()方法中,您會影響一個new Group()和一個new Device() 創建new Device() ,將調用Device.clear()方法並創建一個new ArrayList<Subscription>() ,該方法將調用方法Subscription.clear() ,這將影響new Group()和一個new Device()等。 您無限循環,這就是為什么您得到StackOverflow錯誤的原因。 奇怪的是,您的訂閱是由Group組成的,Group由訂閱的ArrayList組成,而ArrayList也由Groups組成。

也許您有一個循環參考...

Object a = X;
Object c = Y;

a.prop = Y;
c.prop2 = X;

然后,您的JSON(a)是

{
    prop : {
      prop2 : { 
       prop : Y (Y have prop2 again)
       /*infinite..*/
      }
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM