我发誓我已经用尽了我的搜索,并且已经工作了 2 天。我正在尝试使用包含 JSON 对象和嵌入式数组的 Volley Put 请求。
我正在使用 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(View view) {
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 会很有帮助。我真的试过我的大脑了。谢谢。