Symptoms: When POSTing a valid JSONObject (89 characters deserialized) to an Ubuntu Apache Slim PHP (http://www.slimframework.com, https://github.com/slimphp/Slim) server, both Stetho and AnalyticsListener say that 89 bytes was sent, but the server sees only an empty/null body.
When I make what I believe to be the exact same request using Volley the server responds just fine as expected.
(I have yet to get Stetho to work w/ Volley, so I cannot confirm how identical the request is or isn't)
public String requestJSONObject(
final String tag,
@NonNull
Uri uri,
HashMap<String, String> bodyParameterMap)
{
JSONObject jsonObjectBody;
if (bodyParameterMap == null)
{
jsonObjectBody = null;
}
else
{
jsonObjectBody = new JSONObject();
for (Entry<String, String> entry : bodyParameterMap.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
try
{
jsonObjectBody.put(key, value);
}
catch (JSONException e)
{
// ignore
}
}
}
ANRequest request = AndroidNetworking.post(uri.toString())
.setTag(tag)
.addHeaders("User-Agent", mUserAgent)
.addHeaders("APP-ID", mAppId)
.addHeaders("Debug", "true")
.addJSONObjectBody(jsonObjectBody)
.build();
request.setAnalyticsListener(new AnalyticsListener()
{
@Override
public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache)
{
Log.e(TAG, " timeTakenInMillis : " + timeTakenInMillis);
Log.e(TAG, " bytesSent : " + bytesSent);
Log.e(TAG, " bytesReceived : " + bytesReceived);
Log.e(TAG, " isFromCache : " + isFromCache);
}
});
request.getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener()
{
@Override
public void onResponse(Response okHttpResponse, JSONObject response)
{
Log.i(TAG, "requestJSONObject(tag=" + tag + ") onResponse: response=" + response);
//...
}
@Override
public void onError(ANError anError)
{
Log.w(TAG, "requestJSONObject(tag=" + tag + ") onError: anError=" + Utils.toString(anError));
//...
}
});
return tag;
}
Uri uri = Uri.parse("http://api.redacted.com")
.buildUpon()
.appendPath("oauth2")
.appendPath("token")
.build();
HashMap<String, String> postBodyParameters = new HashMap<>();
postBodyParameters.put("grant_type", "refresh_token");
postBodyParameters.put("refresh_token", refreshToken);
requestJSONObject("mytag", uri, postBodyParameters);
Request URL:http://api.redacted.com/oauth2/token
Request Method:POST
Status Code:400 Bad Request
Request Headers:
Accept-Encoding:gzip
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:95
Content-Type:application/json; charset=utf-8
Host:api.redacted.com
APP-ID:redacted
User-Agent:Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 5 Build/LMY48B); com.redacted.testapp 1.0
Request Payload:
{"refresh_token":"redacted","grant_type":"refresh_token"}
Response Headers:
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:close
Content-Length:101
Content-Type:application/json; charset=utf-8
Date:Tue, 08 Nov 2016 22:34:26 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
OkHttp-Received-Millis:1478644376230
OkHttp-Sent-Millis:1478644375903
Pragma:no-cache
Server:Apache/2.4.7 (Ubuntu)
Set-Cookie:PHPSESSID=au8iauf08b2i3qnu4nbrd40ts6; path=/
X-Powered-By:PHP/5.5.9-1ubuntu4.20
Response Payload:
{"error":[{"message":"Post body is empty.","transaction_id":"redacted"}]}{"post_body":null}