如何为Mapbox API的Volley Put请求格式化JSON

时间:2017-01-27 02:31:21

标签: android json android-volley mapbox mapbox-marker

我发誓我已经用尽了我的搜索并且已经工作了2天。我正在尝试使用包含JSON对象和嵌入式数组的Volley Put Request。

我正在使用Mapbox API中的示例:https://www.mapbox.com/api-documentation/#insert-or-update-a-feature

我需要发送的内容:

curl "https://api.mapbox.com/datasets/v1/therefore/lkjashdglkhasdkljsa/features/test123456?dfalkjhsfdlkjhdfs" \
  -X PUT \
  -H "Content-Type: application/json" \
  -d @file.geojson

{
  "id": "test123456",
  "type": "Feature",
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [ 100, 0 ],
        [ 101, 0 ],
        [ 101, 1 ],
        [ 100, 1 ],
        [ 100, 0 ]
      ]
    ]
  },
  "properties": {
    "prop0": "value0"
  }
}

到目前为止我工作的代码能够将新的数据集发布到服务器,但我的意图是在地图上添加一个新的点功能。这是我的创作......

//这会在服务器上创建一个新数据集     public void postMethod(查看视图){

    final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

    StringRequest stringRequest = new StringRequest(Request.Method.PUT, postdatapointurl,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonResponse = new JSONObject(response).getJSONObject("form");
                        String site = jsonResponse.getString("site"),
                                network = jsonResponse.getString("network");
                        System.out.println("Site: "+site+"\nNetwork: "+network);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    textview.setText(response);
                    requestQueue.stop();
                }
            }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            textview.setText("something went wrong");
            error.printStackTrace();
            requestQueue.stop();
        }
    })

    {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("id","nate123456789");
            params.put("geometry",{"type","Polygon") // <-- obviously wrong. This is where the problem is.
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/application.json");
            //headers.put("", "value");
            return headers;
        }
    };
    Volley.newRequestQueue(this).add(stringRequest);


}

正确地格式化JSON并将PUT设置为正确将非常有用。我真的试过了脑子。感谢。

1 个答案:

答案 0 :(得分:0)

我们支持Mapbox Android Services库中的GeoJSON对象。有了它,您可以以编程方式(例如Feature)创建Feature.fromGeometry(Geometry geometry)对象,或者从原始geojson字符串创建Feature geo = Feature.fromJson(geojson); assertEquals(geo.getType(), "Feature"); assertEquals(geo.getGeometry().getType(), "Point"); 对象(以解析服务器响应):

innerHTML

很遗憾,我们仍然不支持数据集API,但我们会在https://github.com/mapbox/mapbox-java/issues/101上跟踪其实施情况。