Android Retrofit和Rxandroid的使用

Retrofit

Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordinary Java Object,简单Java对象),POJO to JSON,网络请求(POST,GET,PUT,DELETE等)封装。

Rxandroid
Rx是响应式编程的意思, 本质是观察者模式,
是以观察者(Observer)和订阅者(Subscriber)为基础的异步响应方式. 在Android编程时, 经常会使用后台线程,请看下面示例:

/** * 数据解析实体 */
public class IpInfo {
    public String country;
    public String country_id;
    public String area;
    public String area_id;
    public String ip;
}
public class BaseResponse {
    public int code;
}

public class GetIpInfoResponse extends BaseResponse {
    public IpInfo data;
}
/** * 作者:cover on 2016/5/25 11:22 * 邮箱:[email protected] */
public class ServiceFactory {
    public static final String BASE_URL = "http://ip.taobao.com";
    private Retrofit mRetrofit;
    private static ServiceFactory mInstance;
    private ApiService apiService;

    public static ServiceFactory getmInstance() {
        if (mInstance == null) {
            synchronized (ServiceFactory.class) {
                mInstance = new ServiceFactory();
            }
        }
        return mInstance;
    }

    private ServiceFactory() {
        createSignRetrofit();
    }
    //mRetrofit 初始化
    private void createSignRetrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    public ApiService getapiService() {
        if (apiService == null) {
            apiService = mRetrofit.create(ApiService.class);
        }
        return apiService;
    }
}
/** * 接口配置 */
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);

}

最后在Activity(根据你需要的情况而定)中调用

public class MainActivity extends AppCompatActivity {

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

    @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);
        //得到实例
        serviceFactory=ServiceFactory.getmInstance();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                serviceFactory.getapiService().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);
                            }
                        });
            }
        });
    }


}

添加依赖包
compile ‘io.reactivex:rxandroid:1.1.0’
compile ‘io.reactivex:rxjava:1.1.0’
compile ‘com.squareup.retrofit:retrofit:2.0.0-beta2’
compile ‘com.squareup.retrofit:converter-gson:2.0.0-beta2’
compile ‘com.squareup.retrofit:adapter-rxjava:2.0.0-beta2’

你可能感兴趣的:(响应式)