Fast-android-networking: post method with Content-type

Created on 25 Aug 2017  ·  11Comments  ·  Source: amitshekhariitbhu/Fast-Android-Networking

用post请求发现Content-Type: application/x-www-form-urlencoded,然后参数就传递不上去,不应该是Content-Type: application/json;charset=UTF-8 吗???

Most helpful comment

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
                    }
                });

All 11 comments

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 void postAsy(final Object tag, TypeToken typeToken, String hostUrl, String methodUrl, Map body_params, final CallBack callback) {
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 body_params before is Map and it just as well the map contain the int tye data ,so the request cannot delever the params to the server

final,the problem is solved.

but I have one question
Wht a Map can request Map,and it is working,did not Make a mistake

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 (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
                    }
                });

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stridercheng picture stridercheng  ·  6Comments

igabice picture igabice  ·  8Comments

alitong picture alitong  ·  8Comments

SunryTeang picture SunryTeang  ·  4Comments

MohadFarhan picture MohadFarhan  ·  5Comments