【问题标题】:Java - Why stackoverflow error in my JSON method?Java - 为什么我的 JSON 方法中出现 stackoverflow 错误?
【发布时间】:2016-02-26 14:06:28
【问题描述】:

我需要你的帮助来理解为什么在我的方法中对象分配返回堆栈溢出错误。

方法如下:

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();
    }

}

这里是设备类:

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.StackOverflowError 在 java.lang.reflect.Executable.(Unknown Source) at java.lang.reflect.Method.(未知来源)在 java.lang.reflect.Method.copy(Unknown Source) 在 java.lang.reflect.ReflectAccess.copyMethod(未知来源)在 sun.reflect.ReflectionFactory.copyMethod(未知来源)在 java.lang.Class.copyMethods(未知来源)在 java.lang.Class.getMethods(未知来源)在 com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:930) 在 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)

感谢大家的帮助!

【问题讨论】:

  • 你试过调试你的应用程序吗?
  • 你能显示更多的堆栈跟踪你能看到重复的部分吗?

标签: java json


【解决方案1】:

可能是您的对象中的循环引用。

【讨论】:

    【解决方案2】:
    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&lt;Subscription&gt;(),这将调用Subscription.clear() 方法,这将影响new Group()new Device() 等等。您正在无限循环,这就是您收到 StackOverflow 错误的原因。奇怪的是,您的订阅是由一个由订阅的 ArrayList 组成的 Group 组成的,这些订阅也是由 Group 等组成的。

    【讨论】:

    • @pedro.olimpio 已更新,检查您是否可以解决您的模型
    • 感谢@Jorel 的帮助,我正在更改此处的循环引用...非常感谢。
    • @pedro.olimpio 很高兴我能为您提供帮助。干杯哥们!
    【解决方案3】:

    也许你有一个循环引用...

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

    那么,你的 JSON(a) 是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2014-10-02
      • 2013-12-28
      • 1970-01-01
      相关资源
      最近更新 更多