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了。
相关文章推荐
- 调试神经网络
- [原创]在使用SDK 23(6.0)版本后org.apache.http相关的类找不到的解决办法
- 网络封包分析工具Charles
- 网络:HTTP 状态码
- 2016-04-20 for http协议
- 王高利:TCP/IP 端口号大全
- UNP(一):网络编程角度下的TCP、UDP协议
- HTTP响应头和请求头信息对照表
- 深入理解CNN的细节
- http://netfilter.org/ 读后感
- 网络中出现TCP、UDP粘包、分包的两点解决办法
- TCP 可靠性
- Retrofit2源码分析
- SUSE学习(网络配置)
- TCP协议的学习(六)滑动窗口 停止等待 退后N帧 选择重传
- 未配置TCP/IP,必须安装并启用网络适配器
- HttpServletResponse,HttpServletRequest详解
- TCP加速–dpdk实践
- JSP报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 网络请求错误