您的位置:首页 > 理论基础 > 计算机网络

retrofit-rxAndroid-最快最好用的网络请求框架

2016-04-18 16:48 537 查看

A type-safe HTTP client for Android and Java

<一>简单使用:首先我们先引入相关的jarandroid  studio 引用方式如下:
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
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'
compile 'de.greenrobot:eventbus:2.4.0'
添加完我们要使用的引用jar之后,我们就可以去使用retrifit:来实现网络数据请求了
<二>我们先写一个接口类:
public interface ApiService {@GET("service/getIpInfo.php")Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);@GET("service/getIpInfo.php")Observable<GetIpInfoResponse> getIpInfo2(@Query("ip") String ip);}
上面的是根据不用的需求来定义不同的接口,其实他就是我们所说的api,这里使用函数的方式来定义,是不是看上去舒服多了呢。
如果要直接获取HTTP返回的对象,使用  [code]Response
  对象。[/code]
然后写一个 我们最为属性的model类
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;}
之后写一个返回的回调类这里这类返回的是一个对象就是我们刚才写的ipinfo类,这类我们自定义一个返回的code,这里我们直接继承就ok了
public class GetIpInfoResponse extends BaseResponse {public IpInfo data;}
然后我们来看看在activity 是如何去使用的。
public class MainActivity extends AppCompatActivity {private static final String ENDPOINT = "http://ip.taobao.com";private TextView mTvContent;private ProgressBar mProgressBar;@Overrideprotected 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() {@Overridepublic 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("223.255.152.98");call.enqueue(new Callback<GetIpInfoResponse>() {@Overridepublic void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) {mProgressBar.setVisibility(View.GONE);GetIpInfoResponse getIpInfoResponse = response.body();mTvContent.setText(getIpInfoResponse.data.country);}@Overridepublic void onFailure(Throwable t) {mProgressBar.setVisibility(View.GONE);mTvContent.setText(t.getMessage());}});/*   Retrofit retrofit = new Retrofit.Builder().baseUrl(ENDPOINT).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();ApiService apiService = retrofit.create(ApiService.class);apiService.getIpInfo("223.255.152.98").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<GetIpInfoResponse>() {@Overridepublic void onCompleted() {mProgressBar.setVisibility(View.GONE);}@Overridepublic void onError(Throwable e) {mProgressBar.setVisibility(View.GONE);mTvContent.setText(e.getMessage());}@Overridepublic void onNext(GetIpInfoResponse getIpInfoResponse) {mTvContent.setText(getIpInfoResponse.data.country);}});*/}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overrideprotected void onDestroy() {super.onDestroy();/*反注册EventBus */BaseApplication.getInstance().unRegister(this);}}
这里就已经完成了我们的初步使用了。是不是很简单呢?记得在清单文件加上
<uses-permission android:name="android.permission.INTERNET"/>
布局就不贴了就一个按钮添加点击事件就ok了。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: