Android-->Retrofit 2.0 beta2 使用方法
来源:互联网 发布:逆波兰算法 java 编辑:程序博客网 时间:2024/05/21 15:45
1.首先
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
2.声明接口
public interface GetBaidu{ @GET("http://www.baidu.com/") Call<ResponseBody> get(); //Call<T> get();//必须是这种形式,这是2.0之后的新形式//我这里需要返回网页内容,不需要转换成Json数据,所以用了ResponseBody;//你也可以使用Call<GsonBean> get();这样的话,需要添加Gson转换器...后续介绍 }
3.调用接口
//经过测试: baseUrl必须设置,如果 声明接口时@GET使用了完整的url路径,那么baseUrl就会被忽略,否则就是拼接urlRetrofit retrofit = new Retrofit.Builder().baseUrl("http://www.baidu.com/").build();//在这里可以添加 Gson转换器等; GetBaidu getBaidu = retrofit.create(GetBaidu.class);//使用上面声明的接口创建 Call<ResponseBody> call = getBaidu.get();//获取一个Call,才可以执行请求//同步请求.... try { Response<ResponseBody> bodyResponse = call.execute(); String body = bodyResponse.body().string();//获取返回体的字符串 Log.e(TAG, ""); } catch (IOException e) { e.printStackTrace(); }//异步请求.... call.enqueue(new Callback<ResponseBody>() {//异步 @Override public void onResponse(Response<ResponseBody> response, Retrofit retrofit) { try { String body = response.body().string();//获取返回体的字符串 } catch (IOException e) { e.printStackTrace(); } Log.e(TAG, ""); } @Override public void onFailure(Throwable t) { Log.e(TAG, ""); } });
未完待续….
参考文章:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0915/3460.html
版权声明:欢迎转载,转载请注明出处-->http://blog.csdn.net/angcyo QQ群:274306954
0 0
- Android-->Retrofit 2.0 beta2 使用方法
- Android-->Retrofit 2.0 beta2 使用方法
- Android Retrofit 2.0
- Android Retrofit 2.0使用
- Android Retrofit 2.0使用
- Retrofit的简单使用方法
- Retrofit的简单使用方法
- Retrofit基本使用方法
- Android Retrofit 2.0 配置OkHttpClient
- 【Android】Retrofit 2.0 的使用
- Silverlight 2.0beta2发布
- Android Studio 1.1 Beta2 发布
- Android Retrofit 2.0的基本使用
- Framework of Android: retrofit (2.0)
- Android Retrofit 2.0框架上传图片解决方案
- android Retrofit 2.0网络请求库
- Android Retrofit 2.0自定义Converter(JSONObject Converter)
- Android框架Retrofit 2.0学习笔记
- 【SpringMVC学习05】SpringMVC中的参数绑定总结
- windows自动开关机
- ManyToMany双向外键关联(基于注解)的映射案例(简单版)
- 从Hadoop URL中读取数据
- UISegmentedControl的基本用法
- Android-->Retrofit 2.0 beta2 使用方法
- 常用效果(六) : 格式化json 日志
- 别人的复制文本记录一下
- PHP之双向链表(SplDoublyLinkedList)简介
- webx之XSS攻击
- Java泛型
- 查看C++类内存布局----Visual studio
- xx后台管理系统的jsp+servlet+connection连接数据库的原始代码
- 毫秒数转日期