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的更多相关文章

  1. android网络框架Retrofit 同步异步

    http://blog.csdn.net/jiguangcanhen/article/details/39006197 同步的方式: 1)首先定义要接口.注解Get表示使用的Get请求方式,{user ...

  2. Android网络框架Volley(体验篇)

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  3. Android网络框架Volley

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  4. Android网络框架Volley(实战篇)

      之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...

  5. Android网络框架-Volley实践 使用Volley打造自己定义ListView

    这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...

  6. ym—— Android网络框架Volley(终极篇)

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ...

  7. Android网络框架之Retrofit + RxJava + OkHttp 变化的时代

    1.什么是Retrofit框架? 它是Square公司开发的现在非常流行的网络框架,所以我们在导入它的包的时候都可以看到这个公司的名字,目前的版本是2. 特点: 性能好,处理快,使用简单,Retrof ...

  8. Android网络框架源码分析一---Volley

    转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...

  9. Android网络框架比较

    今天,公司需要为一个安卓app选择一个合适的网络框架,具体我了解,主要的安卓网络框架有okhttp,retrofit,android-async-http,volley. 查找网上的资料,大致可以得到 ...

随机推荐

  1. Oauth认证简介

    Oauth是什么: 1.Oauth是一种安全认证的协议: 2.Oauth为用户资源的授权提供了一个安全的.开放而又简易的标准: 3.Oauth的授权不会使第三方触及到用户的账号信息(用户名和密码). ...

  2. nginx+php,502错误

    502错误基本就是php进程执行中挂了,其中有个原因就可能是进程执行超时设置导致的比如这个: ; The timeout for serving a single request after whic ...

  3. CPU风扇故障导致自动关机

    今天在使用电脑时,突然自动关机,重启后过一段时间又自动关机,于是打开机箱后盖,插上电源观察各个部位运行情况,发现CPU风扇不转,判断问题就是由于CPU温度太高了.于是换个风扇,再开机情况就正常了.

  4. js 代码记录

    window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...

  5. strcpy实现

    #include <iostream> using namespace std; char *strcpy(char *strDest, const char *strSrc) { if ...

  6. Jquery 简单的Tab选项卡特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. php开发环境安装配置(1)

    个人记录高手请勿喷! 下载xampp我这是个中文版的可以自己搜索下载安装别的版本也行. 双击下载的xampp会提示路径相当于解压到指定的路径 到对应路径去可看到如下: 打开 2.配置: 成功之后会如下 ...

  8. linux学习笔记---一些有趣的命令

    一 在说链接之前我们哈需要说明一个东西,就是inode,一个文件的名字可以有多个,但是inode里的i-number却只有一个,(inode是一个数据结构,里面存放文件的各种属性,属主,属组,权限,大 ...

  9. 转:Google技术开发指南:给大学生自学的建议

    原文来自于:http://blog.jobbole.com/80621/ 技术开发指南 想要成为成功的软件工程师,必须拥有坚实的计算机科学的基础.本指南针对大学生,给出一条自学途径,让学生以科班和非科 ...

  10. Windows Phone 8学习 启动器

    1.发邮件 EmailComposeTack email=new EmailComposeTask(); email.To="收件人"; email.Subject="标 ...