将json对象发送到android中的服务器

时间:2018-04-13 22:10:15

标签: android json server

我正在尝试使用此JSON格式将json发送到HTTP服务器:

{" deviceID":3852883413434," depth":2," location":{" xCord":46.232,&#34 ; yCord":42.4421}"大小":3}

这是我在android中的代码,但似乎效果不好。也许我需要数组位置或格式化我的代码。任何adivce都可以帮助谢谢。

  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();
    }

1 个答案:

答案 0 :(得分:1)

使用json响应,

  

{“deviceID”:3852883413434,“depth”:2,“location”:{“xCord”:46.232,   “yCord”:42.4421}, “尺寸”:3}

您必须获取JSON对象以获取depthsize的值(包含在同一对象中),然后获取location的JSON对象以获取{的值{1}},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();
        }