0

Most of the answers on SO on the subject revolve around sending all your data inside one JSONObject, with the JSONArrays inside.

I would like to do the opposite, if possible.

Here's some code:

    JSONObject winnerJSONObject = new JSONObject();
    JSONObject loserJSONObject = new JSONObject();

try{
            winnerJSONObject.put(Columns.ID.toString(), winner.getId());
            winnerJSONObject.put(Columns.NAME.toString(), winner.getName());
            winnerJSONObject.put(Columns.SCORE.toString(),winner.getScore());
            winnerJSONObject.put(Columns.WINS.toString(), winner.getWins());
            winnerJSONObject.put(Columns.LOSSES.toString(), winner.getLosses());
            winnerJSONObject.put(Columns.MAX_SCORE.toString(),winner.getMaxScore());
            winnerJSONObject.put(Columns.MIN_SCORE.toString(),winner.getMinScore());

            loserJSONObject.put(Columns.ID.toString(), loser.getId());
            loserJSONObject.put(Columns.NAME.toString(), loser.getName());
            loserJSONObject.put(Columns.SCORE.toString(),loser.getScore());
            loserJSONObject.put(Columns.WINS.toString(),loser.getWins());
            loserJSONObject.put(Columns.LOSSES.toString(),loser.getLosses());
            loserJSONObject.put(Columns.MAX_SCORE.toString(),loser.getMaxScore());
            loserJSONObject.put(Columns.MIN_SCORE.toString(),loser.getMinScore());
} catch (JSONException e) {
e.printStackTrace(); 
}


    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse httpResponse = null;

    try {
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(new StringEntity(jsonArray.toString(), HTTP.UTF_8));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        httpResponse = httpClient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONArray jsonArray = new JSONArray();
    jsonArray.put(winnerJSONObject);
    jsonArray.put(loserJSONObject);

Why is this a wrong approach?

4
  • I don't think this should be any problem. Did you try it? Are you getting any error?? Commented Jan 5, 2015 at 21:35
  • I did not try it, because 1: I read people saying it shouldn't be done like that (none of them explained why) 2: I don't know how to get to each JSONObject in PHP after using json_decode (was previously using BasicNameValuePair<T> instead of json) Commented Jan 5, 2015 at 21:56
  • 1
    Of course we shouldn't. But if you want you can do that. Sending array as a parameter allows hackers to push its own data very easily into your parameter. That's why passing array is not advisable, but possible. Commented Jan 5, 2015 at 21:58
  • Use volley ! Saves you loads of code + time Commented Jan 6, 2015 at 1:15

1 Answer 1

1

Yes it is possible.

Example:

Like if we have our data in arraylist to upload on server, Yo can send it in this way

JsonArray _array = new JsonArray()

for(i = 0; i< _arraylist.size(); i++){
JsonObject obj = new JsonObject();

obj.put(_array.list.get(i).getValue);

_array.put(obj);
}

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

Comments

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.