用post请求发现Content-Type: application/x-www-form-urlencoded,然后参数就传递不上去,不应该是Content-Type: application/json;charset=UTF-8 吗???
I use post method ,than find Content-Type: application/x-www-form-urlencoded, and the server cannot delever my params .and the default content type is application/json;charset=UTF-8??
If you want to make a request with POST method, you will need to use the one of three methods (addApplicationJsonBody addJSONObjectBody addJSONArrayBody).
User user = new User();
user.firstname = "Amit";
user.lastname = "Shekhar";
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createUser")
.addApplicationJsonBody(user) // posting java object
.setTag("test")
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
// do anything with response
}
@Override
public void onError(ANError error) {
// handle error
}
});
@zongkang try the above example from @johnwatsondev
ok,thanks ! I want to tell you one intersting thing. on the begin ,I post one requet to the server,the server return the message that the params is error,than ,I analayis ths log,find the content-type ,you know ,before I know the Content-Type: application/x-www-form-urlencoded a little,I thouht is the content-type problem,find not,because in my app,the login api is also ok,so I try to find the params ,finally,I find .addBodyParameter(body_params)
@Override
public
showMock(hostUrl);
AndroidNetworking.post(hostUrl + methodUrl)
.addHeaders("Content-Type","application/json")
.addBodyParameter(body_params)
.addBodyParameter(createDefault())
.setTag(tag)
.setPriority(Priority.MEDIUM)
.build()
.getAsOkHttpResponseAndParsed(typeToken, new OkHttpResponseAndParsedRequestListener
@Override
public void onResponse(Response okHttpResponse, Result<T> response) {
Object tag = okHttpResponse.request().tag();
Log.d(TAG, "onResponse: " + okHttpResponse.toString());
handleOnResponse(callback, response, tag);
}
@Override
public void onError(ANError anError) {
if (anError.getErrorDetail().equals(ANConstants.REQUEST_CANCELLED_ERROR)) {
return;
}
handleOnError(callback, anError, tag);
}
});
}
Map
final,the problem is solved.
but I have one question
Wht a Map
thanks
is it possible to use 'PUT' method instead 'Upload' which uses 'POST' underlying?
Its required in my case.
Currently no.
not working to me
If you want to make a request with POST method, you will need to use the one of three methods (
addApplicationJsonBodyaddJSONObjectBodyaddJSONArrayBody).User user = new User(); user.firstname = "Amit"; user.lastname = "Shekhar"; AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createUser") .addApplicationJsonBody(user) // posting java object .setTag("test") .setPriority(Priority.MEDIUM) .build() .getAsJSONArray(new JSONArrayRequestListener() { @Override public void onResponse(JSONArray response) { // do anything with response } @Override public void onError(ANError error) { // handle error } });
Hi @johnwatsondev , @amitshekhariitbhu !
what's the difference in using those 3 methods mentioned from just using .addBodyParameter in POST method?
hi when iam trying to set contentType application/x-www-form-urlencoded in method addHeader it didnt work i must put it in method setContentType why
This library have lots of issues with Headers, i am replacing in my project with Retrofit. so much frustrated by using this library
Please check all the API's, You can make all type of requests and set your own Content-Type
Most helpful comment
If you want to make a request with POST method, you will need to use the one of three methods (
addApplicationJsonBodyaddJSONObjectBodyaddJSONArrayBody).