Saturday, May 2, 2015

This article is the 15th day of the entry of Hatena engineers Advent calendar 2014. Yesterday id: I

[Read the Retrofit] how to make [Rifukureshon] of Java library that provides an implementation to the interface that the user has defined - Hidamari hfcc socket does not break
This article is the 15th day of the entry of Hatena engineers Advent calendar 2014. Yesterday id: It was by chris4403 "whether made what you think in the development camp."
Code of Retrofit listed in this article, hfcc such as caveat, is what is published under the Apache License, hfcc Version 2.0. The master branch of article writing I am referring to the most recent commit. Those who want to read the code of Retrofit in Android app developers think I may be the easier it is to open up the project of Retrofit in Android Studio. hfcc
Retrofit is a REST client library that is being developed by Square. If you define an interface with a method for executing the HTTP request, Retrofit for us is to provide an implementation of that interface. Annotations by, you can specify that such whether, or a request body designated and any argument to the HTTP method is poorly hfcc parameters.
The following is a sample code * 1 of REST client to get the public timeline of Hatena Haiku in Groovy hfcc script. You have specified hfcc in the GET annotation types and request destination of the path of the HTTP method. And, to be able to change the query parameters to each request, it adapted to receive the value of the query parameters as arguments. Grab ('Com.squareup.retrofit: retrofit: 1.8.0') define an interface with methods that throw the import retrofit.RestAdapter import retrofit.http.GET import retrofit.http.Query // HTTP request. hfcc interface HaikuService {/ ** Hatena to get the public timeline of Haiku * /GET ("/api/statuses/public_timeline.json") List <?> getPublicTimeline (Query ("page") int page,Query ( "count") int count)} // Get the implementation of the interface that you defined. RestAdapter restAdapter = new RestAdapter.Builder () .setEndpoint ("") .build () HaikuService haiku = restAdapter.create (HaikuService. Class) using the // acquired implementation to display the contents hfcc of the public timeline of haiku. def statuses = haiku.getPublicTimeline (1, 10) for (def status: statuses) {println '---' println println status.text}
Proxy class is a class for creating a dynamic proxy class (the class that implements the specified interface at runtime), and is what is also the superclass of dynamic class. Processing of the actual method call is responsible is an instance of a class that implements the InvocationHandler interface. By using the Proxy class, hfcc it is possible hfcc to provide an implementation for any interface at runtime.
/ ** Create an implementation of the API defined by the specified {code service} interface * /SuppressWarnings ("unchecked") public <T> T create (Class <T> service) {Utils.validateServiceClass (service).; return (T) Proxy.newProxyInstance (<?> service.getClassLoader (), new Class [] {service}, hfcc new RestHandler (getMethodInfoCache (service)));}
SuppressWarnings ("Unchecked") //Override public Object invoke (Object proxy, Method method, final Object hfcc [] args) throws Throwable {/ * (abbreviation) * / // Load or create the details cache for the current method. final RestMethodInfo methodInfo = getMethodInfo (methodDetailsCache, method); if (methodInfo.isSynchronous) {try {return invokeRequest (requestInterceptor, methodInfo, args);} catch (RetrofitError error) hfcc {/ * (abbreviation) * /
First of all, it receives the information for the specified method by getMethodInfo (Method) method call as RestMethodInfo object, to enter the actual HTTP request processing is then call the invokeRequest method. In fact, whether synchronous execution or asynchronous execution, hfcc or will or whether in the divided processing is using RxJava, hfcc not-spot so the sides of the details here.
.. private ResponseType parseResponseType () {// Synchronous methods have a non-void return type // Observable methods hfcc have a return type of Observable Type returnType = method.getGenericReturnType (); // Asynchronous methods should have a Callback type as the last . <?>; argument Type lastArgType = null Class lastArgClass = null; Type [] parameterTypes = method.getGenericParameterTypes (); if (parameterTypes.length> 0) {Type typeToCheck = parameterTypes [parameterTypes.length - 1]; lastArgType = typeToCheck ; if (typeToCheck instanceof ParameterizedType) {typeToCheck hfcc = ((ParameterizedType) typeToCheck) .getRawType ();} if (typeToCheck instanceof Class) {lastArgClass = (<?> Class) typeToCheck;}} / * (abbreviation) * /
In the case of synchronous execution returns hfcc the result of the HTTP request as the return value of the method, since in the case of asynchronous execution so that the callback object is passed hfcc to the argument, the return value of the method by calling the following methods hfcc and I am sure both the formal hfcc argument. The return value of information acquisition: Method # getGeneticReturnType () method parameter of information hfcc acquisition: Method hfcc # getGeneticParameterTypes () method
id: nobuoka is I will or writing a daily talk or write the story of the information system
Android Apps (43) C # (2) Cocos2d-x hfcc (1) DOM (2) ECMAScript (5) ElasticSearch (3) Firefox extension (5) Git (3) Google Adsense (1) Gradle hfcc (9) Groovy (3) howto (7) HTML (2) HTTP (1) Internet Explorer (5) Java (18) Java EE (4) JavaScript (28) JAX-RS (3) Jenkins hfcc (1) Learning (5) Linux (1) Metro style app (4) MySQL (1) PC (1) Perl (9) Release notes (5) RFC (2) Ruby (6) SQL (2) SQLite (1) TypeScript (8) Ubuntu (1) Unicode (1) Vim (1) Visual Studio (3) Web application (4) Windows (1) Windows 8 (11) Windows 8.1 (2) Windows hfcc Runtime (2) Windows Store app (23) WinJS (4) algorithm (1) security (1) Software Testing (1) Software Development (1) Practice (8) license (1) library (5) Reviews (9) Virtual Machine (2) study group (10) notice (1) morphological analysis (1) daily and non-daily ( 3) Books (14) Book Review (11) development environment (10) literary miscellany (4)
Powered by Hatena Blog

No comments:

Post a Comment