使用Retrofit和RxJava
生活随笔
收集整理的這篇文章主要介紹了
使用Retrofit和RxJava
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用Retrofit和RxJava整合訪問網絡,然后將數據顯示到界面上
def retrofitVersion = '2.0.0-beta1'dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:22.2.1'compile 'com.android.support:design:22.2.1'//Retrofitcompile "com.squareup.retrofit:retrofit:$retrofitVersion"compile "com.squareup.retrofit:converter-gson:$retrofitVersion"//RxJavacompile 'io.reactivex:rxjava:1.0.14'compile 'io.reactivex:rxandroid:1.0.1' }在app.buidle里面添加ReTrofit和RxJava的依賴,在
dependencies上面一定要注明Retrofit的版本號 def retrofitVersion = '2.0.0-beta1'MainActivity里面的代碼: public class MainActivity extends Activity implements View.OnClickListener {/*** 自定義的觀察者*/public MyObserver observer;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) findViewById(R.id.btn);btn.setOnClickListener(this);observer = new MyObserver();//創(chuàng)建一個觀察者 }//點擊按鈕就請求網絡 @Overridepublic void onClick(View view) {if (view.getId() == R.id.btn) {//點擊獲取好友動態(tài)Control control = new Control(this);control.getFriendsShareFromServer();//訪問網絡并且解析Json}}/*** 自定義的觀察者*/class MyObserver implements rx.Observer<Resquest_friends_info> {@Overridepublic void onCompleted() {Log.d("msg", "觀察的事件結束了---");}@Overridepublic void onError(Throwable e) {Log.d("msg", "觀察的事件出錯了");}//訂閱觀察者后,被觀察者會把數據傳回來@Overridepublic void onNext(Resquest_friends_info resquest_friends_info) {Log.d("msg", "觀察者OnNext");ArrayList<Resquest_friends_info.EveryShareInfo> results = resquest_friends_info.getResult();Toast.makeText(MainActivity.this,"觀察者收到了數據",Toast.LENGTH_SHORT).show();for (Resquest_friends_info.EveryShareInfo info : results) {//每條分享的信息Log.d("msg", "分享信息++++" + info.getPub_context() + "--->" + info.getPub_datetime() + "----->" + info.getPub_frd_name());Log.d("msg", "-----------------------------------------------------");for (Resquest_friends_info.EveryShareInfo.Reply reply : info.pub_com) {//每條回復Log.d("msg", "評論+++++" + reply.getPc_name() + "--->" + reply.getPc_txt() + "--->");Log.d("msg", "----------------------------------------------------------------");}for (Resquest_friends_info.EveryShareInfo.Thumb thumb : info.pub_thumup) {//每個點贊Log.d("msg", "點贊++++" + thumb.getPt_name());Log.d("msg", "---------------------------------------");}}}} } Resquest_friends_info表示一個JavaBean對象,
app結構如下:
//訪問網絡的接口 public interface GitHubService { // ================================================ = = == ==========
//
//表示Get請求,里面是地址這是寫死的地址,但是地址中的參數要動態(tài)改變,就不能這樣寫// @GET("/index.php?m=home&c=message&a=resquest_friends_info&uid=1234567&p=1")
//參數要動態(tài)傳進去,所以要這樣寫
//@GET(value = "/index.php"), 或者@GET("/index.php")也可以 @GET("/index.php") //用這個方法去訪問網絡
Call<Resquest_friends_info> getFriendsShareInfo(@Query("m") String m,@Query("c") String c,@Query("a") String a,@Query("uid") String uid,@Query("p") String p); }
這里的路徑可以自己修改,比如,寫成這樣也是可以的:
//@GET("/index.php?m=home")
//Call<Resquest_friends_info> getFriendsShareInfo(@Query("c") String c,@Query("a") String a,@Query("uid") String uid,@Query("p") String p); ? /**此類是訪問網絡的Control* Created by xhj on 15-12-18.*/ public class Control {public static final String TAG="msg";
//URL根路徑,一般就是域名public static final String APITrack="http://192.168.1.102";private MainActivity activity;/**構造方法時把觀察者所在的類傳進來*/public Control(MainActivity activity){this.activity=activity;}/**從服務器獲取好友動態(tài)*/public void getFriendsShareFromServer(){Retrofit retrofit = new Retrofit.Builder().baseUrl(APITrack).addConverterFactory(GsonConverterFactory.create())//用Gson去解析數據.build();GitHubService git = retrofit.create(GitHubService.class);Call<Resquest_friends_info> call = git.getFriendsShareInfo();call.enqueue(new Callback<Resquest_friends_info>() {
//訪問網絡回來,并且成功拿到數據就調用這個方法@Overridepublic void onResponse(Response<Resquest_friends_info> response) {final Resquest_friends_info resquest_friends_info = response.body();Observable<Resquest_friends_info> observable = Observable.create(new Observable.OnSubscribe<Resquest_friends_info>() {@Overridepublic void call(Subscriber<? super Resquest_friends_info> subscriber) {subscriber.onNext(resquest_friends_info);subscriber.onCompleted();//事件結束 }});observable.subscribe(activity.observer);//訂閱觀察者 }@Overridepublic void onFailure(Throwable t) {Log.d("msg","失敗了");}});} }
?
轉載于:https://www.cnblogs.com/android-yus/p/5062724.html
總結
以上是生活随笔為你收集整理的使用Retrofit和RxJava的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蘑菇街怎么微信支付
- 下一篇: 交易密码和支付密码有什么区别