Okhttp-based Asynchronous requests

Source: Internet
Author: User
Tags getmessage gettext

Take the time to encapsulate a okhttp-based network request, just encapsulate the get,post, download,upload request, do not need to create a thread, support JSON automatic conversion.

Get and post (file upload also belongs to post) are divided into two main steps:

1. Create the request entity object through Httprequestbuilder.

2. Send the request via the Httputil SendRequest method, and use Resultcallback to accept the return content, which automatically converts the JSON to an entity when successful in this step, and the OnError method of Resultcallback If the failure is performed , and pass HttpException, which contains HTTP error codes, such as 404,500.

Get Request Method:

private void Getsample () {HttpRequest request = new Httprequestbuilder (). URL (URL). AddHeader ("Cookie", "DF"). Addparams ( "Key", "value"). Method (Httpmethod.get). build (); Httputil.getinstance (). SendRequest (Request, new resultcallback<result> () {@Overridepublic void Onresponse ( Result response) {//TODO auto-generated method Stubtextview.settext (Response.getdata (). GetName ());} @Overridepublic void OnError (Request request, HttpException HttpException) {//TODO auto-generated method Stubtextview.settext ("Requestcode:" +httpexception.getresponsecode () + "errormessage:" +httpexception.getmessage () );}});}

POST request:

private void Postsample () {HttpRequest request = new Httprequestbuilder (). URL ("http://www.oschina.net/action/user/ Hash_login "). AddHeader (" Cookie "," DF "). Addparams (" email "," xxxxxxxxx "). Addparams (" pwd "," xxxxxxxxxxxxxxxxx "). Method (Httpmethod.post). build (); Httputil.getinstance (). SendRequest (Request,new resultcallback<string> () {@Overridepublic void OnError ( Request request,httpexception httpexception) {//TODO auto-generated method stub} @Overridepublic void Onresponse (String Response) {//TODO auto-generated Method Stubtextview.settext (response);}});}


File Upload:

private void Fileuploadsample () {if (! Textutils.isempty (Textview.gettext ())) {File File = new file (Textview.gettext (). toString ()); final String URL = "Server address ... "; HttpRequest request = new Httprequestbuilder (). URL (URL). Method (Httpmethod.post). AddFile (new filediscription (file). Setuploadprograsslistener (Fileuploadlistener)). Build (); Httputil.getinstance (). SendRequest (Request,new resultcallback<string> () {@Overridepublic void OnError ( Request request,httpexception httpexception) {//TODO auto-generated method stub} @Overridepublic void Onresponse (String Response) {//TODO auto-generated Method Stubtextview.settext (response);}});}}


The file download is a GET request, but instead of using Resultcallback, Downloadcallback is used to listen for download progress, complete and failed callbacks. The code is as follows:

private void DownLoad () {HttpRequest request= new Httprequestbuilder (). URL ("http://img.taopic.com/uploads/allimg/ 130501/240451-13050106450911.jpg "). Method (Httpmethod.get). Downloadpath (FilePath, string.valueof (New Date (). GetTime ()) + ". jpg"). Build (); Httputil.getinstance (). DownLoad (Request, new Downloadcallback () {@Overridepublic void ondownloading (int prograss) {// TODO auto-generated Method Stubtextview.settext (prograss+ "%");p rogressbar.setprogress (Prograss);} @Overridepublic void Ondownloadsuccess () {//TODO auto-generated method Stubtextview.settext ("Down load Success");} @Overridepublic void Ondownloadfail (HttpException ex) {//TODO auto-generated method Stubtextview.settext ( Ex.getmessage ());});}

Source Address:

Https://github.com/YounMario/HttpDemo

Okhttp-based Asynchronous requests

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.