0

I am trying to send an array with a bunch of objects back to android?

I tried String Builder like so:

StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(int i = 0; i <usersChanged.size(); i++) {
            DatabaseUser userChange = usersChanged.get(i);

                if(userChange.getIsFollowingType() == 0) {
                    String userIdStr = "{userId:" +  userChange.getUserId() + ",";
                    String followingStr = "following:" +  String.valueOf(userChange.getIsFollowingType())+ "}]";
                    sb.append(userIdStr).append(followingStr);

but I am doing something wrong here. On my server side I am using node.js and would parse the array if I send over a string no problem, but this is not sending a string version of the array? What do I need to change on my string builder? Or is there a more efficient way to send over the list of usersChanged - (which is List)

I am using retrofit if there is a way to do it easy with that.

5
  • Which side you are facing problem?? server Side or android side?? Commented May 11, 2016 at 2:35
  • android side, I am looking for a solution to send over an array Commented May 11, 2016 at 2:37
  • StringBuilder?? use android.util.JsonWriter instead Commented May 11, 2016 at 5:35
  • What is the difference? Efficiency or? Commented May 11, 2016 at 6:05
  • efficiency, no need for reinventing the wheel and lack of bugs Commented May 11, 2016 at 6:13

1 Answer 1

1

Your code will send things like

[{userId: someuserid, following: someparameter}]

You need to have the double quote (" ") sent over as well.

for(int i = 0; i <usersChanged.size(); i++) {
        DatabaseUser userChange = usersChanged.get(i);

            if(userChange.getIsFollowingType() == 0) {
                String userIdStr = "{\"userId\":\"" +  userChange.getUserId() + "\",";
                String followingStr = "\"following\":\"" +  String.valueOf(userChange.getIsFollowingType())+ "\"}]";
                sb.append(userIdStr).append(followingStr);

You should also probably move the "]" to after the loop.

EDIT:

Forgot to mention this, but you can definitely use JSONArray and JSONObject to make your life easier.

JSONArray jsonArray = new JSONArray();
for(...){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("userId", userChange.getUserId());
    jsonObject.put("following", String.valueOf(userChange.getIsFollowingType()));
    jsonArray.put(jsonObject);

}

Then send over the jsonArray.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks will try that out now, yeah I have an if else statement just copied it out the code is a lot heavier but that is what I forgot.
@Lion789 the start with [ is fine, [{"userId":"asdf", "following":"asdf"}] is a valid JSON.
@Lion789 have updated the answer with other alternative.
I dont think i can send back a jsonArray with retrofit unfortunately.
You can try converting that to string first. i.e. String s = jsonArray.toString();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.