Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonObject Build Stackoverflow error

I am trying to build a jsonobject to be send with a post request. But instead i get the error as shown below:

03-02 13:54:50.340: E/AndroidRuntime(2862): FATAL EXCEPTION: main
03-02 13:54:50.340: E/AndroidRuntime(2862): Process: in.mally.mallys, PID: 2862
03-02 13:54:50.340: E/AndroidRuntime(2862): java.lang.StackOverflowError
03-02 13:54:50.340: E/AndroidRuntime(2862):     at java.lang.String._getChars(String.java:908)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at java.lang.StringBuilder.append(StringBuilder.java:216)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at org.json.JSONStringer.beforeValue(JSONStringer.java:412)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at org.json.JSONStringer.open(JSONStringer.java:178)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at org.json.JSONStringer.object(JSONStringer.java:158)
03-02 13:54:50.340: E/AndroidRuntime(2862):     at org.json.JSONObject.writeTo(JSONObject.java:670)

The Error seems to a stackoverflow error. I can't quite figure out how that works. The code below attempts to build a jsonObject and puts that Object into another Jsonobject as a value. The format for JsonObject i am trying to make is

{ "email" : "[email protected]",
  "user_record" : first_jsonObject
}

First JsonObject is just a simple key-value pair of strings. Below is the code that i used to acheive it.

JSONObject j = new JSONObject();
    try {
        j.put("offer_id", "531328f8616c69133a350000");
        j.put("views", "1");
        j.put("call_count", "0");
        j.put("rating", "3");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JSONObject json = new JSONObject();
    try {
        json.put("email", "[email protected]");
        json.put("user_post_records", json);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    JsonObjectRequest r = new JsonObjectRequest(Method.POST,POST_URL, json, 
            new Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // TODO Auto-generated method stub

                }
            }, 
            new ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }
            });
    VolleyRequest.getInstance(getActivity().getBaseContext()).getRequestQueue().add(r);
like image 539
thestrongenough Avatar asked Mar 02 '14 19:03

thestrongenough


1 Answers

You are trying to set a json object as a value to itself. Hence, it calls itself which becomes an invalid recursive call and hence the StackOverflowError.

It's presumably a typo from your end, hence:

Change:

json.put("user_post_records", json);

To:

json.put("user_post_records", j);
like image 187
StoopidDonut Avatar answered Nov 11 '22 22:11

StoopidDonut