1

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.

  • Tried to run your code with a simple replacement of the JSONObjects. Worked fine. Try to log the size of the array before running the for loop, just to make sure it is 295 items long. – SilverCorvus Aug 29 '15 at 02:42
  • Ok so it seems that after the loop to construct the JSONArray there is indeed 295 items inside of it so I don't understand why when I use the toString() method on the whole JSONArray, it just prints out the beginning of the JSONArray and cut in the middle of a JSONObject. Because of that my server cannot understand the received JSON content and I cannot store my data in my db... The JSONObject that I store inside of my JSONArray are from type long, int, string and Date, if that can helps! – Björn Järnsida Aug 29 '15 at 13:33
  • can you give an example of a json string for one of your object? I assume `{object1}` is a simplification, since it's not a valid JSON string. – SilverCorvus Aug 29 '15 at 14:02
  • @CurlyCorvus, I improved my question :) – Björn Järnsida Aug 29 '15 at 14:23
  • Do you have a debugger? What is the value of `jSMSArray.toString()` in the debugger? (Not the logs) – SilverCorvus Aug 29 '15 at 14:48
  • I just use logcat I never dealt with debuggers I don't know how to use them :/ – Björn Järnsida Aug 29 '15 at 14:50
  • What a better time to start than when you write your master thesis? https://developer.android.com/tools/debugging/debugging-studio.html It will greatly ease debugging – SilverCorvus Aug 29 '15 at 15:01
  • I am going to take a look at that thanks :) – Björn Järnsida Aug 29 '15 at 15:05

1 Answers1

1

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)

Community
  • 1
  • 1
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
  • It seems that the issue doesn't come from there, because on my server side I receive the exact same truncated Array... :/ I really don't know where that can come from. I tried everything I could think about in order to debug this! – Björn Järnsida Aug 29 '15 at 16:25
  • Now, it is more likely that an error occurs at the creation of the JSONArray and that the structure of the Array is not properly formatted in JSON so when I invoke the method toString() on it there is a problem. – Björn Järnsida Aug 29 '15 at 17:57
  • Indeed after 4 days of digging this problem, I tried to print out the content of my Array in a file in the server side instead of in the logcat and i get the whole content. So logcat does not print out the JSONArray properly! :) – Björn Järnsida Aug 31 '15 at 09:08