Android uses the Asynchttpclient post method to send data can be broadly divided into two situations;
post(android.content.Context context, java.lang.String url, cz.msebera.android.httpclient.HttpEntity entity, java.lang.String contentType, ResponseHandlerInterface responseHandler)
This is the format of the parameters of the post can be customized, the general use of JSON, for details see (http://stackoverflow.com/questions/26842090/ Asynchttpclient-passed-contenttype-will-be-ignored-because-httpentity-sets-cont) illustrate
Asynchttpclient client =NewAsynchttpclient ();FinalString URL = utils.baseurl+"Regist.html"; Jsonobject Jsonobject =NewJsonobject ();Try{Jsonobject.put ("username", Etphonenumber.gettext ()); Jsonobject.put ("Password", Etpassword.gettext ()); }Catch(Jsonexception e) {E.printstacktrace (); } bytearrayentity entity =NULL;Try{entity =NewBytearrayentity (Jsonobject.tostring (). GetBytes ("UTF-8")); Entity.setcontenttype (NewBasicheader (HTTP. Content_Type,"Application/json")); }Catch(Unsupportedencodingexception e) {E.printstacktrace (); } client.post (Mcontext,url,entity,"Application/json",NewJsonhttpresponsehandler () {@Override Public void onsuccess(intStatusCode, header[] headers, jsonobject response) {Super. onsuccess (StatusCode, headers, response); LOG.E ("RS", response.tostring ()); }@Override Public void onfailure(intStatusCode, header[] headers, throwable throwable, Jsonobject errorresponse) {Super. OnFailure (StatusCode, headers, throwable, errorresponse); } });
The other is to use
postparams, ResponseHandlerInterface responseHandler)
This post method, this is the regular price increase form format, the format of the parameters for Username=yzk&password=yzk this. Examples of Use
Final Requestparams params = new Requestparams ();Params. Put("username", Etphonenumber. GetText());Params. Put("Password", Etpassword. GetText());Client. Post(Url,params,new Jsonhttpresponsehandler () {@Override public void onsuccess (int statu SCode, header[] headers, jsonobject response) {Super. onsuccess(StatusCode, headers, response);try {Log. E("RS", response. getString("Status"));if (response. getString("Status"). Equals("0")) {user = user. Getinsstance();User. Setisonline(true);User. Settoken(Response. getString("token"));User. SetId(Etphonenumber. GetText(). toString());Toast. Maketext(Mcontext,"Registered successfully", Toast. LENGTH_long). Show();Mcontext. Finish();}else{Toast. Maketext(Mcontext,"Use registered", Toast. LENGTH_long). Show();}} catch (Jsonexception e) {E. Printstacktrace();}} @Override public void onfailure (int statusCode, header[] Head ERs, Throwable throwable, Jsonobject errorresponse) {Super. OnFailure(StatusCode, headers, throwable, errorresponse);Toast. Maketext(Mcontext,"Network Error", Toast. LENGTH_long). Show();}
This is actually a problem encountered in the game project, the beginning of the server is written to parse the JSON format, and I use the second post method, so the error, but also the son of the above to spend some time, the server code to deal with non-JSON format, At that time, I felt that asynchttpclient could not provide the JSON transfer method, and then calm down to see, special in this summary.
Asynchttpclient Post method uses