Android應用架構之Retrofit、RxAndroid使用
阿新 • • 發佈:2018-12-30
上篇部落格客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類如下
3、修改MainActivity類如下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); }
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