Use retrofit for network requests under Android Studio

Source: Internet
Author: User
<span id="Label3"></p>Environmental requirements <ul> <ul> <li>Android version 2.3 and above</li> <li>Java version 6 and above</li> </ul> </ul>Configuration <ul> <ul> <li>Adding references under the Gradle configuration file</li> </ul> </ul><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r">dependencies { <span class="hljs-keyword">...</span> <span class="hljs-string">‘com.squareup.retrofit:retrofit:1.9.0‘</span> <span class="hljs-keyword">...</span>}</code></pre></pre> <ul> <ul> <li>When Okhttp exists, retrofit uses okhttp to make a network request, using Okhttp's Add Reference as follows</li> </ul> </ul><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r">dependencies { <span class="hljs-keyword">...</span><span class="hljs-string">‘com.squareup.okhttp:okhttp-urlconnection:2.0.0‘</span><span class="hljs-string">‘com.squareup.okhttp:okhttp:2.0.0‘</span> <span class="hljs-keyword">...</span>}</code></pre></pre>Initiating a request<p><p>The following example is a simple asynchronous get request that uses retrofit to return a value of type string</p></p><p><p>First define an interface that declares a method in the interface to define the content of the request</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs d"> <span class="hljs-keyword">interface</span> User { <span class="hljs-keyword">@GET</span>(<span class="hljs-string">"/user/list.json"</span>) <span class="hljs-keyword">void</span> getUsers(<span class="hljs-keyword">@Query</span>(<span class="hljs-string">"pagesize"</span><span class="hljs-keyword">int</span> pagesize, Callback<String> callback); }</code></pre></pre> <ul> <ul> <li><p>@GET indicate that the request is get mode, in addition to the @post, @PUT, @DELETE, and @head, the specific role please refer to the official documentation, due to space reasons, no longer described here.</p></li> <li><p>@Query for parameter declarations</p></li> <li><p>callback< string > is the callback interface, string indicates that the returned data result is a string type</p></li> </ul> </ul><p><p>Then create a Restadapter object with the following code</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">RestAdapter restAdapter = new RestAdapter<span class="hljs-preprocessor">.Builder</span>() <span class="hljs-preprocessor">.setEndpoint</span>(CTX)<span class="hljs-preprocessor">.setConverter</span>(new BaseConverter()) <span class="hljs-preprocessor">.build</span>()<span class="hljs-comment">;</span></code></pre></pre> <ul> <ul> <li><p>The setEndPoint method specifies the first half of the request address, which is the server address, such as <strong>https://api.github.com</strong></p></li> <li><p>The Setconverter method requires an implementation class for the transformation interface as a parameter, the function of which is to convert the requested InputStream to the desired type, the sample code is as follows</p></li> </ul> </ul><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">baseconverter</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Converter</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Object<span class="hljs-title"><span class="hljs-title">Frombody</span></span>(typedinput body, Type Type)<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>Conversionexception {bytearrayoutputstream BAOs =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Bytearrayoutputstream ();<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>I =-<span class="hljs-number"><span class="hljs-number">1</span></span>; String response =<span class="hljs-string"><span class="hljs-string">""</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{<span class="hljs-keyword"><span class="hljs-keyword"></span> while</span>((i = body.in (). Read ())! =-<span class="hljs-number"><span class="hljs-number">1</span></span>) {baos.write (i); } response = Baos.tostring (); Baos.close (); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(ioexception E) {e.printstacktrace (); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Response }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Typedoutput<span class="hljs-title"><span class="hljs-title">Tobody</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Object Object) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Typedoutput () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>String<span class="hljs-title"><span class="hljs-title">FileName</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>String<span class="hljs-title"><span class="hljs-title">MimeType</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-string"><span class="hljs-string">"String"</span></span>; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Long</span></span> <span class="hljs-title"><span class="hljs-title">length</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Object = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>?<span class="hljs-number"><span class="hljs-number">0</span></span>: object.tostring (). Length (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">WriteTo</span></span>(outputstream Out)<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>IOException {out.write (object.tostring (). getBytes ()); } }; }}</code></pre><p><p>When a Restadapter object is obtained, call the Create method of Restadapter to get the implementation class of the user Interface.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-keyword">new</span> Callback<String>() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">success</span>(String s, Response response) { } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">failure</span>(RetrofitError error) { } } User user = getAdapter().create(User.class); <span class="hljs-comment">//指定请求参数和回调接口的实现类</span> user.getUsers(<span class="hljs-number">12</span>,callback);</code></pre></pre> <ul> <ul> <li>The strength of retrofit is that it gives the interface a method annotation based on the interface</li> </ul> </ul>Code obfuscation<p><p>If you need code obfuscation, configure the following</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs haml">-<span class="ruby">dontwarn retrofit.**</span>-<span class="ruby">keep <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">retrofit</span>.** { *;</span> }</span>-<span class="ruby">keepattributes <span class="hljs-constant">Signature</span></span>-<span class="ruby">keepattributes <span class="hljs-constant">Exceptions</span></span></code></pre></pre> <p><p>Use retrofit for network requests under Android Studio</p></p></span>

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.