Android 网络框架--Retrofit
1、导入Jar包
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
2、创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder()
.baseUrl("请求基地址/")
//可以接收自定义的Gson
//Retrofit会使用Gson将ResponseBody
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ServerInterface.BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())//解析成字符串
.build(); 3、定义请求接口 public interface IRegister {
@POST("接口路径")
@FormUrlEncoded //键值对
Call<请求原型RegisterBean> register(@Field("请求参数Key user.name") String username, @Field("user.passWord") String password); }
public interface IUpdate {
//完善资料,需要上传用户头像
@POST(ServerInterface.USER_UPDATE)
@Multipart //表单数据
//文件上传,文件不带Part()
Call<String> userUpdate(@Part MultipartBody.Part doc,
@Part("user.id")String userId,
@Part("user.userName")String nickName,
@Part("user.sex")String sex,
@Part("user.myInfo")String info);
} 4、创建请求对象
IRegiter service = retrofit.create(IRegister.class)
Call<RegisterBean> call = service.register(username, password);
文件上传
File file = new File(path文件地址);
//将文件写入body请求内容体
//HTTP contentType 对照表
RequestBody body = RequestBody.create(MediaType.parse("image/jpg"),file);
//使用内容体去创建一个表单对象 image/*表示所有图片
MultipartBody.Part doc = MultipartBody.Part.createFormData("doc",file.getName(),body);
Call<String> call = retrofit.create(IUpdate.class).userUpdate(doc, userId, nickName, sex, myInfo);
5、执行
使用okHttp的的执行流程、 同步 堵塞线程 Response<RegisterBean> execute = call.execute();
异步 call.enqueue(new Callback<RegisterBean>(){回调};
取消 call.cancel();
Retrofit注解 Retrofit 共22个注解
1.HTTP请求方法
方法注解同Http请求模式 GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS,
可用HTTP来代替上面七个方法,HTTP有三个属性method ,path ,hasBody 。
2.Http请求类型
FormUrlEncoded Http默认请求方式,键值对。application/x-www-from-urlencoded
Multipart 带文件上传 from-data
Streaming 流类型返回
3.请求参数
Headers 添加请求头
Header 添加不固定值的Header
Field Post普通键值对
FiledMap Post普通键值对
Part Post文件上传
PartMap Post多文件上传
Query Get请求
QueryMap Get请求
Url
用于URL
Retrofit转换
GsonConverterFactory
当使用该转换器时,自动将返回json转换成Bean对象,但如果需要获取字符串会出错,需要使用下面转换器
ScalarsConverterFactory
官方提供的字符串转换工具
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
Android 网络框架--Retrofit的更多相关文章
- android网络框架Retrofit 同步异步
http://blog.csdn.net/jiguangcanhen/article/details/39006197 同步的方式: 1)首先定义要接口.注解Get表示使用的Get请求方式,{user ...
- Android网络框架Volley(体验篇)
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- Android网络框架Volley
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- Android网络框架Volley(实战篇)
之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...
- Android网络框架-Volley实践 使用Volley打造自己定义ListView
这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...
- ym—— Android网络框架Volley(终极篇)
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ...
- Android网络框架之Retrofit + RxJava + OkHttp 变化的时代
1.什么是Retrofit框架? 它是Square公司开发的现在非常流行的网络框架,所以我们在导入它的包的时候都可以看到这个公司的名字,目前的版本是2. 特点: 性能好,处理快,使用简单,Retrof ...
- Android网络框架源码分析一---Volley
转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...
- Android网络框架比较
今天,公司需要为一个安卓app选择一个合适的网络框架,具体我了解,主要的安卓网络框架有okhttp,retrofit,android-async-http,volley. 查找网上的资料,大致可以得到 ...
随机推荐
- Java 可变参数
java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理.注意:可变参数必须位于最后一项.当可变参数个数多余一个时,必将有一个不是最后一项,所以只支持 ...
- 人人网FED CSS编码前端开发规范
文件相关规范 1.文件名必须由小写字母.数字.中划线-组成 2.文件必须用utf-8编码 3.文件引入可通过外联或内联方式引入: 3.1 外联方式:<link rel=”stylesheet” ...
- spring 构造注入 异常 Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments
你可能在做项目的时候,需要在项目启动时初始化一个自定义的类,这个类中包含着一个有参的构造方法,这个构造方法中需要传入一些参数. spring提供的这个功能叫“构造注入”, applicationCon ...
- 使用Python操作Redis
1. 安装pyredis 首先安装pip 1 2 3 4 5 6 7 8 <SHELL># apt-get install python-pip ...... <SHELL> ...
- [r]Seven habits of effective text editing
Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...
- ACM竞赛常用STL(一)
全排列函数next_permutation STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespac ...
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序【转】
0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...
- Python 字典 Print 格式化
__author__ = 'dell' ab = {'Swaroop': 'swaroopch@byteofpython.info', 'Larry': 'larry@wall.org', 'Mats ...
- BZOJ 1501 智慧珠游戏
Description Input 文件中包含初始的盘件描述,一共有10行,第i行有i个字符.如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对 ...
- [POJ 2774] Long Long Message 【后缀数组】
题目链接:POJ - 2774 题目分析 题目要求求出两个字符串的最长公共子串,使用后缀数组求解会十分容易. 将两个字符串用特殊字符隔开再连接到一起,求出后缀数组. 可以看出,最长公共子串就是两个字符 ...