3

I'm trying to send a JSON object which contains a JSON array with JSON objects inside via HTTP POST parameters.

The format of the parameter (what the server expects) is something like:

{""team"":[
    {""teamid"":""179228"",""position"":1},
    {""teamid"":""218036"",""position"":2},
    {""teamid"":""88109"",""position"":3},
    {""teamid"":""88111"",""position"":4},
    {""teamid"":""165536"",""position"":5},
    {""teamid"":""224645"",""position"":6}
]}

nevertheless, what gets sent is:

{"team":"[
\"{\\\"position\\\":0,\\\"teamid\\\":\\\"88107\\\"}\",\"{\\\"position\\\":1,\\\"teamid\\\":\\\"88109\\\"}\",\"{\\\"position\\\":2,\\\"teamid\\\":\\\"156714\\\"}\",\"{\\\"position\\\":3,\\\"teamid\\\":\\\"138877\\\"}\",\"{\\\"position\\\":4,\\\"teamid\\\":\\\"168730\\\"}\",\"{\\\"position\\\":5,\\\"teamid\\\":\\\"88110\\\"}\",\"{\\\"position\\\":6,\\\"teamid\\\":\\\"88111\\\"}\",\"{\\\"position\\\":7,\\\"teamid\\\":\\\"134431\\\"}\",\"{\\\"position\\\":8,\\\"teamid\\\":\\\"88112\\\"}\",\"{\\\"position\\\":9,\\\"teamid\\\":\\\"138507\\\"}\",\"{\\\"position\\\":10,\\\"teamid\\\":\\\"138880\\\"}\",\"{\\\"position\\\":11,\\\"teamid\\\":\\\"138881\\\"}\",\"{\\\"position\\\":12,\\\"teamid\\\":\\\"151465\\\"}\",\"{\\\"position\\\":13,\\\"teamid\\\":\\\"151464\\\"}\
"]"}

The way I build that JSON object is the following:

            JSONArray teamArray = new JSONArray();
            JSONObject jsonRoot = new JSONObject();
            for (int i = 0; i < mTeams.size(); i++) {
                String teamId = null;
                BaseModel data = mTeams.get(i);
                if (data != null && data instanceof TeamModel) {
                    teamId = ((TeamModel) data).getId();
                }
                JSONObject teamObject = new JSONObject();
                try {
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsPosition), i);
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsTeamId), teamId);
                    teamArray.put(teamObject);
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            try {
                jsonRoot.put("team", teamArray);
                mNameValuePairs.put("teams", jsonRoot);
                    } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

In the last but one line (jsonRoot.put("team", teamArray);) it has the same format as what gets sent in the last line, but with one less \, so one less times "parsed" apparently.

Part of my HTTP code:

String postBody = json.toString();
Log.d("HTTPHelper", "posting JSON: " + postBody);
((HttpPost) httpRequest).setEntity(new StringEntity(postBody));

Why is this happening? Is it Java? Any ideas how could I build the correct JSON? or any work around?

Thanks a lot in advance!

noloman
  • 11,411
  • 20
  • 82
  • 129
  • 1
    can we see the end of the code. Also, how are your data in your strings.xml file ? (you shouldn't add any \ or " character). Also, maybe you have some typos in the expected JSON server side : double double-quote is not correct :) – mithrop Jun 27 '13 at 07:55
  • @mithrop the problem is that in iOS, this is working perfectly, but in Android I'm having lots of troubles, so the problem shouldn't be server side... BTW I just added the last part of the code -just a couple of `catch` statements – noloman Jun 27 '13 at 08:01
  • ok great. Can you still answer about the data in strings.xml file ? – mithrop Jun 27 '13 at 08:07
  • yes sorry! there're no \ in the `strings.xml` – noloman Jun 27 '13 at 08:08
  • 2
    Are you sure your problem is not in your HTTP code? Can you show us that too? Perhaps you have a `Content Type` problem? – Ken Y-N Jun 27 '13 at 08:37
  • @KenY-N but when it arrives to the method where I create the HTTP code, all those `\` area already there – noloman Jun 27 '13 at 11:23
  • how you are passing url and other parameters – Harish Jun 27 '13 at 12:29
  • [Here's an example usage](http://docs.database.com/dbcom/en-us/db_intro/quickstart_sample_code_java.htm?version=184.0) - note that `requestBody.setContentType("application/x-www-form-urlencoded")` is used. I'm not sure what `StringEntity` really does, but when I was developing a simple REST API (PHP at the other end) I never used it... Also [see this Q&A on a similar problem](http://stackoverflow.com/questions/6533234/how-to-make-httppost-call-with-json-encoded-body). – Ken Y-N Jun 27 '13 at 15:52

3 Answers3

1

I gave this up, and decided to go the dirty-way: replacing chars manually the following way:

json = new JSONObject(nameValuePairs.toString().replace("\"", "'"));
json = new JSONObject(json.toString().replace("\"", ""));

It's ugly, probably dangerous or risky, but it works...

noloman
  • 11,411
  • 20
  • 82
  • 129
1

Too much code for this task, checkout this library https://github.com/kodart/Httpzoid It uses GSON internally and provides API that works with objects. All JSON details are hidden.

Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();
Arthur
  • 674
  • 1
  • 8
  • 14
0

just a tip... i'm not really into android, just java.. but you could de- and encode your json with Base64 on both sides, and pass the encoded "String".

so you don't have to worry about any "dirty-way" replacing.

hope this helps u or others, too. :)