Android应用架构之Retrofit、RxAndroid使用

     上篇博客客http://blog.csdn.net/liuhongwei123888/article/details/50375283  已经介绍了Retrofit的简单使用方法,接下来介绍的是在Retrofit中怎么使用RxAndroid,如果还不了解RxAndroid的请移步大神“扔物线”的教程  http://gank.io/post/560e15be2dca930e00da1083。


1、将RxJava、RxAndroid引入项目中

compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'

2、修改ApiService类如下

package com.micky.retrofitrxandroiddagger2.data.api;

import com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse;

import retrofit.Call;
import retrofit.http.GET;
import retrofit.http.Query;
import rx.Observable;

/**
 * @Project retrofitrxandroiddagger2
 * @Packate com.micky.retrofitrxandroiddagger2.data.api
 * @Description
 * @Author Micky Liu
 * @Email [email protected]
 * @Date 2015-12-21 17:22
 * @Version 1.0
 */
public interface ApiService {
  /*  @GET("service/getIpInfo.php")
    Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);*/

    @GET("service/getIpInfo.php")
    Observable<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);
}


3、修改MainActivity类如下

package com.micky.retrofitrxandroiddagger2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.micky.retrofitrxandroiddagger2.common.utils.CrashHandler;
import com.micky.retrofitrxandroiddagger2.data.api.ApiService;
import com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * @Project retrofitrxandroiddagger2
 * @Packate com.micky.retrofitrxandroiddagger2
 * @Description
 * @Author Micky Liu
 * @Email [email protected]
 * @Date 2015-12-21 17:35
 * @Version 1.0
 */
public class MainActivity extends AppCompatActivity {

    private static final String ENDPOINT = "http://ip.taobao.com";
    private TextView mTvContent;
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvContent.setText("");
                mProgressBar.setVisibility(View.VISIBLE);

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(ENDPOINT)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                ApiService apiService = retrofit.create(ApiService.class);
                apiService.getIpInfo("63.223.108.42")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<GetIpInfoResponse>() {
                            @Override
                            public void onCompleted() {
                                mProgressBar.setVisibility(View.GONE);
                            }

                            @Override
                            public void onError(Throwable e) {
                                mProgressBar.setVisibility(View.GONE);
                                mTvContent.setText(e.getMessage());
                            }

                            @Override
                            public void onNext(GetIpInfoResponse getIpInfoResponse) {
                                mTvContent.setText(getIpInfoResponse.data.country);
                            }
                        });
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

    OK,代码写完了,还是先测试下吧。我都不信自己写的程序没bug。

   果然,令android程序猿心惊胆战的bug来了,,,程序crash了。看看日志如下

java.lang.IllegalArgumentException: Unable to create call adapter for rx.Observable<com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse>
   

 百思不得其姐之后,只能求助于谷哥帮忙...在build.gradle中增加一个依赖项

compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'


并修改代码:

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(ENDPOINT)
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .build();


    编译运行,It 's perfect...

    切记:版本要一致,我掉过的坑大家就不要去了

     

     

详细代码:http://download.csdn.net/download/liuhongwei123888/9372394


你可能感兴趣的:(Android应用架构之Retrofit、RxAndroid使用)