Asynchttpclient Post method uses

Source: Internet
Author: User
Tags gettext throwable

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.