简体   繁体   中英

JSONArray.toString() not working in Java

I try to send data from my android client to my web server, using JSON.

I am facing an issue. Since now more than two days of debugging, it seems that the problem the toString method of the JSONArray class is not working properly.

I have the constraint that I cannot use the GSON library. Thus, I am dealing only with the JSONArray and JSONObject libraries.

I construct my JSONArray as follows:

ArrayList<SMS> SMSArray = SMSHelper.getSMSDetails(context, rEmail, rPhoneNum);
JSONArray jSMSArray = new JSONArray();

for(int i = 0; i < SMSArray.size(); i++) {
    jSMSArray.put(i, SMSArray.get(i).toJSON());
    Log.wtf("::trace", "JSONArray.get(i).toString(): " + jSMSArray.get(i).toString());
}

Log.wtf("::trace", "JSONArray.toString(): " + jSMSArray.toString());

Inside of the loop, I get the exact same string printed out from my JSONArray and my ArrayList. SO it seems that the copy of the ArrayList into JSONArray works fine. Plus after the loop the size of the two Array are the same.

Nevertheless, when I use JSONArray.toString() after the loop, surprisingly, the string is not complete the string prints only few JSONObject out of 285 JSONObject that contains my JSONArray!

I want to send this JSONARRAY to my server to parse it on the server side but as the JSONArray is not correct. The server cannot parse the received string chain. The received String chain looks like: [{object1}, ..., {objecti}, {"id", $myId, "thread", $myThread, "content", "hey

I really hope that one of you will be able to help me this. It is driving me crazy; first I used the syntax for(Object object: ObjectArray) but then I learnt that a JSONArray is not an iterable object so I tried to direcly put the JSONObject inside a specific index but I got the same outcome.

This code is part of my Master Thesis, and I am loosing a good amount of time on this details... :/

I really, look forward to your answers.

Goodnight,

Björn

EDIT: My JSONObject are the SMS stored in the android mobile phone. My fonction to convert them into JSONObject is the following:

public JSONObject toJSON() {
    JSONObject jSMS = new JSONObject();
    try {
        jSMS.put("id", this.id);
        jSMS.put("thread", this.thread);
        jSMS.put("rEmail", this.rEmail);
        jSMS.put("rPhoneNum", this.rPhoneNum);
        jSMS.put("sPhoneNum", this.sPhoneNum);
        jSMS.put("sName", this.sName);
        jSMS.put("date", this.date);
        jSMS.put("content", this.content);
        jSMS.put("type", this.type);
    } catch (JSONException e) {
        Log.wtf("::trace", Log.getStackTraceString(e));
    }
    return jSMS;
}

EDIT1: When I loop through my JSONArray and use JSONArray.getJSONObject(i).toString() everything works fine it prints me the whole content of the Array, when I call the JSONArray.toString() method it prints me out only the beginning of the JSONArray. I really start to be tired by that. I have no idea what can causes such error.

see What is the size limit for Logcat and how to change its capacity?

Every logcat entry is limited to a certain number of characters (depending on your android device). It is very possible that a JSON array 285 objects long is too long for a single logcat entry, so it just cuts it in the middle. I don't think there is a problem with your json objects (or if there is, it's not the one you found)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM