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. 寻找链表中倒数第K个结点的位置

    输入一个链表,输出该链表中倒数第K个结点. struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* FindKthToTail( ...

  2. laravel5通过auth.attempt事件加入登陆验证码

    <?php namespace WangDong\Http\Controllers\Auth; use Illuminate\Http\Exception\HttpResponseExcepti ...

  3. C#DataTable操作

    ] 在DataSet中添加DataTable DataSet.Tables.Add(DataTable) 实例: DataSet ds=new DataSet(); DataTable table=n ...

  4. PHP获取每月第一天与最后一天

    <?phpfunction getthemonth($date){$firstday = date('Y-m-01', strtotime($date));$lastday = date('Y- ...

  5. 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol

    关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol 2014-02-18 19:57 315人阅读 评论(0) 收藏 举报 1.category, ...

  6. 完美世界-2015校园招聘-java服务器工程师-成都站

    给定一个整数,将该整数分解成多个2的幂次方相加的形式,每次都取最大的可以分解出来的2的幂次方 比如 65 64 1 1 1 2 2 package wanmanshijie; import java. ...

  7. opencv 2.46与visual studio 2012 配置方法

    一开学就搞实训,还是没学过的图像处理.痛苦啊!图像处理时一般使用Matlab中的图像工具箱,或者是C/C++和OpenCV结合使用.以前看过一些关于opencv的文章,没想到现在要用上了. 把搭建开发 ...

  8. Tautology

    WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of th ...

  9. Unity NGUI实现Tabview

    unity版本:4.5.1 NGUI版本:3.5 参考链接:http://blog.csdn.net/g__dragon/article/details/17242969,作者:CSDN G_Drag ...

  10. Effective c++ 第一章 让自己习惯C++

    条款 01:c++是一个语言联邦而不是一种单一的语言, 它包括: 1.C语言:没有模版.没有异常.没有重载…… 2.Object-Oriented C++:class.析构函数.构造函数.封装.继承. ...