Android 2.2でPOSTリクエストを実行する際に問題が発生しています。新しいバージョン4.0以降ではすべて正常に動作します。私が行っているリクエストは、https リクエストでもあります。私はいつも 400 Bad リクエストを受け取ります。私が使用しているコードは次のとおりです。
public String postRequest(String myurl, String requestBody) throws IOException {
try {
//Create connection
URL url = new URL(myurl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
for (Map.Entry<String, String> headerMap : httpHeaders.entrySet()) {
connection.setRequestProperty(headerMap.getKey(), headerMap.getValue());
Log.d("HttpRequest", " headers Key = " + headerMap.getKey() + ", Value = " + headerMap.getValue());
}
connection.setUseCaches (false);
connection.setDoOutput(true);
if(requestBody != null && requestBody.length() > 0) {
connection.setRequestProperty("Content-Length", "" +
Integer.toString(requestBody.getBytes().length));
connection.setFixedLengthStreamingMode(requestBody.getBytes().length);
// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Log.v("RESPONSE MESSAGE", connection.getResponseMessage());
//Send request
OutputStream out = connection.getOutputStream();
out.write(requestBody.getBytes());
out.flush();
out.close();
Log.v("RESPONSE MESSAGE", myurl + " "+ connection.getResponseMessage());
}
int responseCode = connection.getResponseCode();
Log.w("HttpRequest rponse code:", myurl+" - "+responseCode);
if(responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED) {
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} else {
// System.setProperty("http.keepAlive", "false");
InputStream error = ((HttpURLConnection) connection).getErrorStream();
Log.e("HTTP error", "Error, request failed code: "+ responseCode + " "+connection.getResponseMessage() +" \n " + error);
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
また、xml 本文を投稿しています。
<SetAchievementsCompletedByConsoleSpecificKeyRESTXML xmlns='http://tempuri.org/'><platformActionKeys xmlns:ns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><ns:string>1</ns:string></platformActionKeys></SetAchievementsCompletedByConsoleSpecificKeyRESTXML>