我正在尝试编写一个应用程序,该应用程序使用HTTP POST请求向服务器发出请求,并返回JSON数组。除了Volley不会随请求一起发送尸体,所有这些工作都是有效的。我已经看了很多遍,据我所知,我的代码与我可以在网上找到的所有示例均匹配。任何帮助将不胜感激!
private void createPin(LatLng位置,字符串文本,int类型,int范围,最终标记tempMarker){
String pinRequestURL = "https://positive-green-gallinule.anvil.app/_/api/pin";
JSONObject jsonBodyObj = new JSONObject();
//Add everything to main Body
jsonBodyObj.put("lat", position.latitude);
jsonBodyObj.put("lng", position.longitude);
jsonBodyObj.put("text", text);
jsonBodyObj.put("type", type);
jsonBodyObj.put("scope", scope);
final String requestBody = jsonBodyObj.toString();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, pinRequestURL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Do something with response
for (int i=0; i<response.length(); i++) {
try{
JSONObject pin = response.getJSONObject(i);
//Log.d("#OUT", String.valueOf(pin));
}catch (JSONException e){
e.printStackTrace();
}
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
Log.d("#OUT2", "ERROR!");
Log.d("#OUT3", error.toString());
error.printStackTrace();
}
}
){
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(null,null);
return params;
}
@Override
public byte[] getBody() {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
String credentials = "brad@wetpaintdesigns.com:abc";
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
return headers;
}
};
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy( 100000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//Add to request Queue
queue.add(jsonArrayRequest);
}