Use of the "Android-frame" Retrofit+rxjava
Last Update:2016-12-22
Source: Internet
Author: User
<span id="Label3"></p><p><p>A few days ago, retrofit and Rxjava were summarized, this post intends to retrofit combined Rxjava use of the method summarized BELOW. Have not known retrofit or Rxjava friends can refer to the following posts to learn ~</p></p><p><p>Use of the "Android-frame" retrofit</p></p><p><p>Use of the "Android-frame" Rxjava</p></p><p><p></p></p><p><p>Import Dependencies First:</p></p><pre><pre><span style="color: #000000">dependencies { compile filetree (include: [</span>' *.jar '], dir: ' Libs '<span style="color: #000000">) androidtestcompile (</span>' com.android.support.test.espresso:espresso-core:2.2.2 '<span style="color: #000000">, { </span>' com.android.support ', module: ' Support-annotations '<span style="color: #000000"> }) </span>' com.android.support:appcompat-v7:24.2.1 '<span style="color: #000000"> </span>junit:junit : 4.12 '<span style="color: #000000"> </span>com.squareup.retrofit2:retrofit:2.1.0 '<span style="color: #000000"> </span>com.squareup.retrofit2: <span style="color: #000000"> </span>converter-gson:2.1.0 '<span style="color: #000000"> </span>com.squareup.retrofit2:adapter-rxjava:2.1.0 ' io.reactivex:rxjava:1.2.2 ' io.reactivex:rxandroid:1.2.1 '<span style="color: #000000">}</span> <span style="color: #000000"> </span></pre></pre><p><p>Then add the Permissions:</p></p><pre><pre><span style="color: #0000ff"><</span><span style="color: #800000"></span><span style="color: #ff0000">android:name</span><span style="color: #0000ff">= "android.permission.INTERNET"</span><span style="color: #0000ff">/></span></pre></pre><p><p>Before posting the code, declare that the case used in this post is the same as the case in Retrofit's Post.</p></p><p><p></p></p><p><p><strong>Code:</strong></p></p><p><p>The combination of the two code (not encapsulated):</p></p><pre>Retrofit Retrofit =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">retrofit.builder (). baseUrl (shareddata.base_url). addconverterfactory (gsonconverterfact Ory.create ()). addcalladapterfactory (rxjavacalladapterfactory.create ()). Build (); Retrofitservice Service</span></span>= Retrofit.create (retrofitservice.<span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">); Observable</span></span><InfoData> observable =<span style="color: #000000"><span style="color: #000000">Service.getinfodata (); Observable. subscribeon (schedulers.io ()). observeon (androidschedulers.mainthread ()) . Subscribe (</span></span><span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Subscriber<infodata><span style="color: #000000"><span style="color: #000000">() {@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">oncompleted () {} @Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onError (throwable e) {} @Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onNext (infodata infodata) {toast.maketext (mainactivity.</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">, Infodata.getname (), toast.length_short). show (); } });</span></span></pre><p><p>The combination of the two code (encapsulation):</p></p><p><p>Packaged Tool Classes:</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">com.example.itgungnir.testretrofit_rxjava.share.SharedData;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">retrofit2. Retrofit;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">retrofit2.converter.gson.GsonConverterFactory;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">rx. Observable;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">rx. subscriber;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">rx.android.schedulers.AndroidSchedulers;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">rx.schedulers.Schedulers;</span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* Web Access Tool class</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">Httputil {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span><span style="color: #000000"><span style="color: #000000">Httputil instance; </span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span><span style="color: #000000"><span style="color: #000000">Retrofit Retrofit; </span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span><span style="color: #000000"><span style="color: #000000">httputil () {</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> this</span>. Instance =<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">; </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> this</span>. Retrofit =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">retrofit.builder (). baseUrl (shareddata.base_url). addconverterfactory (gsonconverterfact Ory.create ()). addcalladapterfactory (rxjavacalladapterfactory.create ()). Build (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span><span style="color: #000000"><span style="color: #000000">httputil getinstance () {</span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(instance = =<span style="color: #0000ff"><span style="color: #0000ff">NULL</span></span><span style="color: #000000"><span style="color: #000000">) { </span></span><span style="color: #0000ff"><span style="color: #0000ff">synchronized</span></span>(httputil.<span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">) { </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(instance = =<span style="color: #0000ff"><span style="color: #0000ff">NULL</span></span><span style="color: #000000"><span style="color: #000000">) { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span> <span style="color: #0000ff"><span style="color: #0000ff">New</span></span><span style="color: #000000"><span style="color: #000000">Httputil (); } } } </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span><span style="color: #000000"><span style="color: #000000">instance; } </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span><T> T GetService (class<t><span style="color: #000000"><span style="color: #000000">C) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span><span style="color: #000000"><span style="color: #000000">getinstance (). retrofit.create (c); } </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span><T><span style="color: #0000ff"><span style="color: #0000ff">void</span></span>Init (observable<t> Observable, subscriber<t><span style="color: #000000"><span style="color: #000000">Subscriber) {observable. subscribeon (schedulers.io ()). observeon (androidschedulers.mainthread ()) . Subscribe (subscriber); }}</span></span></pre><p><p></p></p><p><p>To invoke the tool class in the main thread:</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span>Mainactivity<span style="color: #0000ff"><span style="color: #0000ff">extends</span></span><span style="color: #000000"><span style="color: #000000">appcompatactivity {@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Httputil.init (httputil.getservice (retrofitservice.</span></span><span style="color: #0000ff"><span style="color: #0000ff">class</span></span>). Getinfodata (),<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Subscriber<infodata><span style="color: #000000"><span style="color: #000000">() {@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">oncompleted () {} @Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onError (throwable e) {} @Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onNext (infodata infodata) {toast.maketext (mainactivity.</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">, Infodata.getname (), toast.length_short). show (); } }); }}</span></span></pre><p><p>Operation Result:</p></p><p><p></p></p><p><p>Use of the "Android-frame" Retrofit+rxjava</p></p></span>