2

I am trying to send json to HTTP server with this JSON format:

{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232, "yCord":42.4421},"size":3}

and here is my code in android, but seems not working well.Maybe i need array for location or format my code.Any adivce could help thanks.

  JSONObject postData = new JSONObject();
    try {
        postData.put("deviceID", unique_id);
        postData.put("depth",((HoleSize)this.getApplication()).getDepth());
        postData.put("xCord",((HoleSize)this.getApplication()).getLattitude());
        postData.put("yCord",((HoleSize)this.getApplication()).getLongitude());
        postData.put("size", ((HoleSize) this.getApplication()).getSize());

        new SendData().execute("servername",postData.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
4
  • What is the variable that store the json response? @IvanHristov Commented Apr 13, 2018 at 22:29
  • location "has no value" is a json object "location":{"xCord":46.232, "yCord":42.4421} Commented Apr 13, 2018 at 22:38
  • i have class that hold depth,size , xCord,Ycord but dont have object location. I should make object in class that has these two values and parse it to json? @Jorgesys Commented Apr 13, 2018 at 23:51
  • ok if you realy wants the object location you can get it with postData.get("location").toString() check my answer! Commented Apr 13, 2018 at 23:59

1 Answer 1

1

Using the json response,

{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232, "yCord":42.4421},"size":3}

you must get the JSON Object to get the value of depth and size (contained in the same object) and then get the JSON Object of location to get the values of xCord, yCord:

      try {

            JSONObject postData = new JSONObject(response);

            String depth = postData.get("depth").toString();
            String size = postData.getInt("size").toString();

            JSONObject locationObject = new JSONObject(postData.get("location").toString());
            String xCord = locationObject.get("xCord").toString();
            String yCord = locationObject.get("yCord").toString();


        } catch (JSONException e) {
             e.printStackTrace();
        }

applying this to your code:

 try {
        JSONObject postData = new JSONObject(response);

        postData.put("deviceID", unique_id);
        postData.put("depth",postData.get("depth").toString());
        //postData.put("xCord", locationObject.get("xCord").toString());
        postData.put("location", postData.get("location").toString());
        postData.put("yCord",locationObject.get("yCord").toString());
        postData.put("size", postData.getInt("size").toString());

        new SendData().execute("servername",postData.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }
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.