我試圖使用Volley庫向JAVA安卓應用程序發送POST請求到Azure IoT Hub,但是出現此錯誤: BasicNetwork.performRequest:意外的響應代碼400爲https://elca-iot.azure-devices.net/devices/elca-main-device/messages/events?api-version=2016-02-03)Android Volley POST請求返回錯誤400(錯誤請求)
要訪問物聯網中心我需要使用一個SAS密鑰,我需要包含在請求的標題。而Android應用程序代碼如下:
RequestQueue queue = Volley.newRequestQueue(c);
String url = "https://elca-iot.azure-devices.net/devices/" + deviceId + "/messages/events?api-version=2016-02-03)";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
onPostResponse(response, c, true);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.getMessage());
onPostResponse(error.getMessage(), c, false);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//params.put("Content-Type", "application/xml");
params.put("Authorization", "[My SAS Key]");
params.put("Cache-Control", "no-cache");
return params;
}
/*
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("api-version","2016-02-03");
return params;
}*/
@Override
public String getBodyContentType() {
return "application/text; charset=UTF-8";
}
@Override
public byte[] getBody(){
return "On".getBytes();
}
};
try {
Log.d("request", String.valueOf(stringRequest.getMethod()==Request.Method.POST));
} catch (Exception authFailureError) {
authFailureError.printStackTrace();
}
// Add the request to the RequestQueue.
queue.add(stringRequest);
我試着做郵差使用相同的請求和它的工作,我不知道爲什麼它不與Android應用程序的工作。下面是與郵差的請求的HTTP代碼:
POST /devices/elca-main-device/messages/events?api-version=2016-02-03 HTTP/1.1
Host: elca-iot.azure-devices.net
Authorization: [My SAS Key]
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: d0aa354a-f79c-e574-893a-c259232aa5cb
on
編輯:
你明確地將Content-Type設置爲'application/text',而郵遞員請求表明它應該是'application/x-www-form-urlencoded',這是Volley的默認值。檢查是否刪除,解決問題 – akash93
我試過這個,它不起作用。 –
你會發送一個屏幕截圖從你的帖子男子@MarcosFelipe –