Closed paulpv closed 7 years ago
FYI, here is my working Volley code:
mRequestQueue.add(new JsonObjectRequest(uri.toString(), jsonObjectBody, new Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Log.i(TAG, "requestJSONObject(tag=" + tag + ") onResponse: response=" + response);
//...
}
}, new ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.w(TAG, "requestJSONObject(tag=" + tag + ") onError: error=" + error);
//...
}
})
{
@Override
public String getBodyContentType()
{
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders()
throws AuthFailureError
{
Map<String, String> headers = super.getHeaders();
if (headers == null || headers.equals(Collections.emptyMap()))
{
headers = new HashMap<>();
}
headers.put("User-Agent", mUserAgent);
headers.put("APP-ID", mAppId);
headers.put("Debug", "true");
return headers;
}
});
I just created the same situation to test it.
API - which require both
When I make request it is working perfectly. Still trying to figure out the problem you are facing.
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("firstname", "Amit");
// jsonObject.put("lastname", "Shekhar");
} catch (JSONException e) {
e.printStackTrace();
}
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createAnUser")
.addJSONObjectBody(jsonObject)
.setTag(this)
.setPriority(Priority.LOW)
.build()
.getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
}
@Override
public void onError(ANError error) {
}
});
Thanks Amit Shekhar
Thanks for the response. Can you clarify your statement? What causes "otherwise it send error with error body as bad parameter", or is everything "it is working perfectly"?
When I make request with correct data, it is giving me in onResponse
method
jsonObject.put("firstname", "Amit");
jsonObject.put("lastname", "Shekhar");
When I make request with wrong data, it is giving me in onError
method with errorBody as {"error":"bad parameter"}
jsonObject.put("firstname", "Amit");
// not setting lastname.
That sounds to me like a server side logic choice that is probably by design. What happens if you post a single field using Volley? I still can't figure out why Volley works, but AN doesn't. :/
Today's investigation is leading me to believe that the problem I am seeing is related to the GzipRequestInterceptor.
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//builder.addInterceptor(new GzipRequestInterceptor());
if (BuildConfig.DEBUG)
{
builder.addNetworkInterceptor(new StethoInterceptor());
}
OkHttpClient httpClient = builder.build();
AndroidNetworking.initialize(applicationContext, httpClient);
When I comment out the GzipRequestInterceptor line, my server logic seems to work. I can confirm that my server used to be set up to accept gzip encoding, but from looking at the headers in my first entry above, the server isn't responding w/ gzip, so perhaps it got disabled. I will confirm w/ my web dev as soon as he recovers from last night's US election...
Yup! The problem is with adding GzipRequestInterceptor when a server have gzip enabled. gzip capabilities need to be tested as supported on the server before enabled. Which begs a general question, which may end up being an okhttp question: How should this negotiation be tested and enabled on a per-server basis?
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)
I believe that my code is pretty straightforward:
Usage:
Stetho sniff:
(I had my web developer output to the response the post body that he read.)
Any idea why AN doesn't work, but Volley does?