网络访问框架经过了从使用最原始的AsyncTask构建简单的网络访问框架(甚至不能称为框架),后来使用开源的android-async-http库,再到使用google发布的volley库,一直不懈的寻找更好的解决方案(银弹),到现在虽然银弹没找到,也算找 到了一些更好的方案,在这里做些记录,也做共享。
目标架构如下图:
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 获取json数据。
先看看庐山真面目,大家看了也会说,酱紫啊,搜森破!
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;
- /**
- * @Project retrofitrxandroiddagger2
- * @Packate com.micky.retrofitrxandroiddagger2.data.api
- * @Description
- * @Author Micky Liu
- * @Email mickyliu@126.com
- * @Date 2015-12-21 17:22
- * @Version 1.0
- */
- 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;
- /**
- * @Project CommonProject
- * @Packate com.micky.commonproject.data.api.response
- * @Description
- * @Author Micky Liu
- * @Email mickyliu@126.com
- * @Date 2015-12-21 16:39
- * @Version 1.0
- */
- 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;
- /**
- * @Project retrofitrxandroiddagger2
- * @Packate com.micky.retrofitrxandroiddagger2
- * @Description
- * @Author Micky Liu
- * @Email mickyliu@126.com
- * @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) {
- 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);
- }
- }
就这么简单,上述代码已经实现通过retrofit访问restful api获取数据并显示。
详细代码请见:http://download.csdn.net/download/liuhongwei123888/9372394