Android Network framework retrofit synchronous asynchronous

Source: Internet
Author: User

http://blog.csdn.net/jiguangcanhen/article/details/39006197

How to synchronize:

1) first define to interface. The comment get indicates how the GET request is used, and {user} represents the data to be replaced

Public interface Githubservice {
@GET ("/users/{user}/repos")
List<repo> Listrepos (@Path ("user") String user);
}

2) Initialize the Restadapter and use the dynamic proxy to create the interface object.

Restadapter restadapter = new Restadapter.builder ()
. setEndPoint ("https://api.github.com")
. build ();
Githubservice service = restadapter.create (Githubservice.class);

3) Use network access and return

list<repo> repos = Service.listrepos ("Octocat");

Asynchronous Way:

@POST ("/users/new")
void CreateUser (@Body user user, callback<user> CB);

Note: Body annotations, the object will be converted by the converter before making the request.

The first two are the same as before, except that the last parameter becomes the callback object.

Controller.getlogin (mail, password, new callback<userdatamodel> () {

@Override

Public void failure (retrofiterror error) {

TODO auto-generated Method stub

}

@Override

Public void success (Userdatamodel LDM, Response Response) {

TODO auto-generated Method stub

});

You can see that callbacks are made in failure and success when the request network returns, and the default data converter converts the corresponding JSON string to an object. (Of course, we can also define our own data converters)

To define your own data converters:

Public class Myconverter implements Converter {

@Override

Public Object Frombody (typedinput body, type type) throws conversionexception {

StringBuffer result = new stringbuffer ();

Try {

InputStream is = body.in ();

byte [] buffer = new byte[1024];

while (Is.read (buffer)! =-1) {

Result.append (new String (buffer, "UTF-8"));

}

} catch (IOException e) {

E.printstacktrace ();

}

return result;

}

@Override

Public Typedoutput Tobody (objectobject) {

return null;

}

}

The above custom data converter, after the request succeeds, returns the string to us instead of the corresponding string that was Gson converted.

Restadapter restadapter = new restadapter.builder ()

. setEndPoint ("url")

. Setconverter (new myconverter ())

. build ();

After creating the Restadapter, create the settings to use your own data converters.

We also have the flexibility to do other configurations:

1) set up the request interceptor to intercept the request before it is issued.

Requestinterceptor requestinterceptor = new Requestinterceptor () {
@Override
public void intercept (Requestfacade request) {
Request.addheader ("User-agent", "Retrofit-sample-app");
}
};
Restadapter restadapter = new Restadapter.builder ()
. setEndPoint ("https://api.github.com")
. Setrequestinterceptor (Requestinterceptor)
. build ();

2) Setting the error controller

Class MyErrorHandler implements ErrorHandler {
@Override public throwable handleError (Retrofiterror cause) {
Response r = Cause.getresponse ();
if (r! = null && r.getstatus () = = 401) {
return new unauthorizedexception (cause);
}
return cause;
}
}
Restadapter restadapter = new Restadapter.builder ()
. setEndPoint ("https://api.github.com")
. Seterrorhandler (New MyErrorHandler ())
. build ();

3) set log log.

Restadapter restadapter = new Restadapter.builder ()
. Setloglevel (RestAdapter.LogLevel.FULL)
. setEndPoint ("https://api.github.com")
. build ();

The above is just a brief introduction of its use, in fact, it has a lot of features, get,post,put,delete,head ... Please see the official website for details.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Network framework retrofit synchronous asynchronous

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.