网络访问框架经过了从使用最原始的AsyncTask构建简单的网络访问框架(甚至不能称为框架),后来使用开源的android-async-http库,再到使用google发布的volley库,一直不懈的寻找更好的解决方案,到现在也没找到,不过据行业所说,Retrofit算是行业比较牛逼的开源请求框架了吧,也算找 到了一些更好的方案,在这里做些记录,也做共享。
下面是我们常用的mvp模式的运行模型:
Retrofit:Square提供的开源产品,为Android平台的应用提供一个类型安全的REST客户端
- https://github.com/square/retrofit
RxAndroid:响应式编程框架RxJava针对Android平台的扩展。
- https://github.com/ReactiveX/RxAndroid
- https://github.com/ReactiveX/RxJava
Dagger2:依赖注入(或叫ioc,用过Spring的都知道这东东)框架,用于解耦的。
- https://github.com/google/dagger/
为了书写 方便,便于实践,我们用下面的地址作为服务器请求地址:http://ip.taobao.com/service/getIpInfo.php?ip=21.22.11.33
接下来我们开始新建一个项目;
1、通过配置gradle配置将Retrofit框架引入项目。
- compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
- compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
2、编写API服务代码
- 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;
-
-
- public interface ApiService {
- @GET("service/getIpInfo.php")
- Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);
- }
3、定义接收数据的response
- package com.micky.retrofitrxandroiddagger2.data.api.response;
-
-
- import com.micky.retrofitrxandroiddagger2.data.api.model.IpInfo;
-
- public class GetIpInfoResponse extends BaseResponse {
- public IpInfo data;
- }
4、请求数据并显示
- 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;
-
-
- 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) {
-
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(ENDPOINT)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- ApiService apiService = retrofit.create(ApiService.class);
-
- mProgressBar.setVisibility(View.VISIBLE);
-
- Call<GetIpInfoResponse> call = apiService.getIpInfo("63.223.108.42");
- call.enqueue(new Callback<GetIpInfoResponse>() {
- @Override
- public void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) {
- mProgressBar.setVisibility(View.GONE);
- GetIpInfoResponse getIpInfoResponse = response.body();
- mTvContent.setText(getIpInfoResponse.data.country);
- }
-
- @Override
- public void onFailure(Throwable t) {
- mProgressBar.setVisibility(View.GONE);
- mTvContent.setText(t.getMessage());
- }
- });
- }
- });
- }
-
- @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);
- }
- }