上一篇文章讲述了Retrofit的简单使用,这次我们学习一下Retrofit的各种HTTP请求.

Retrofit基础

在Retrofit中使用注解的方式来区分请求类型.比如@GET("")表示一个GET请求,括号中的内容为请求的地址.

格式 含义
@GET 表示这是一个GET请求
@POST 表示这个一个POST请求
@PUT 表示这是一个PUT请求
@DELETE 表示这是一个DELETE请求
@HEAD 表示这是一个HEAD请求
@OPTIONS 表示这是一个OPTION请求
@PATCH 表示这是一个PAT请求

基本的HTTP请求

Retrofit可实现基本HTTP请求,包括GET,POST,PUT,DELETE等.

1.GET请求

[代码]java代码:

1
2
@GET("/record")
Call<phoneresult> getResult();</phoneresult>

2.POST请求

[代码]java代码:

1
2
@POST("/record")
Call<phoneresult> getResult();</phoneresult>

3.PUT请求.

[代码]java代码:

1
2
@PUT("/record")
Call<phoneresult> getResult();</phoneresult>

4.DELETE请求

[代码]java代码:

1
2
@DELETE("/record")
Call<phoneresult> getResult();</phoneresult>

服务器接口类型

服务器接口有很多中,本人经验有限,目前接触较多为以下几种:

直接请求型

即直接对某一地址或组合某一地址发起请求

如:对/result和/result/{id}发起GET请求,其中{id}中的id在实际使用时填写实际值即可.

带参查询型

对某一地址进行带参查询请求

如:https://www.baidu.com/s?wd=123为对接口https://www.baidu.com/s进行参数为wd=123的GET查询请求.

带Header型

 即请求时要求带上Header

Retrofit中如何写?

直接请求型

1.如果是直接请求某一地址,写法如下:

[代码]java代码:

1
2
@GET("/record")
Call<phoneresult> getResult();</phoneresult>

2.如果是组合后直接请求,如/result/{id}写法如下:

[代码]java代码:

1
2
@GET("/result/{id}")
Call<phoneresult> getResult(@Path("id") String id);</phoneresult>

带参查询型

如12306的查询接口https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-03-18&from_station=BJP&to_station=CDW,写法如下:

[代码]java代码:

1
2
3
@GET("/otn/lcxxcx/query")
Call<result> query(@Query("purpose_codes") String codes, @Query("queryDate") String date,
    @Query("from_station") String from, @Query("to_station") String to)</result>

带Header型

比如要更新某个账户信息,其接口地址为/info,需要带的Header有设备信息device,系统版本version,还要带请求参数要更新账户的id,代码如下:

[代码]java代码:

1
2
3
@POST("/info")
Call<object> updateInfo(@Header("device") String device, @Header("version") int version,
                        @Field("id") String id); <br></object>

注:本想给每一种请求添加一个请求实例,但是确实不太好找.

实例

找了很久发现多说提供了一些POST请求接口,下面就以多说的接口为例,看一下如何使用Retrofit写请求.

基础URL

多说的接口基础地址为:http://api.duoshuo.com,则构建Retrofit实例代码如下:

[代码]java代码:

1
2
3
4
Retrofit retrofit = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("http://api.duoshuo.com")
        .build();

获取文章评论、转发数

接口地址为:/threads/counts

HTTP请求方式:GET

请求示例为:http://api.duoshuo.com/threads/counts.json?short_name=official&threads=4ff1cbc43ae636b72a00001d

后面的.json为返回数据的格式,此处我们使用json格式.

请求代码如下:

[代码]java代码:

1
2
3
@GET("/threads/counts.json")
Call<object> getCommit(@Query("short_name") String shortName,
                       @Query("threads") String threads); <br></object>

匿名发表新评论

接口地址为:/posts/create

HTTP请求方式:POST

请求示例为:

Request URL:http://api.duoshuo.com/posts/create.json
Request Method:POST
Post Data:short_name=official&author_email=jp.chenyang%40gmail.com&author_name=Perchouli&thread_id=1152923703638301959&author_url=http%3A%2F%2Fduoshuo.com&message=匿名发表新评论

1.Field方式实现

[代码]java代码:

1
2
3
4
5
6
7
8
9
@FormUrlEncoded
@POST("/posts/create.json")
Call<commitresult> createCommit(@Field("secret") String secret,
                                @Field("short_name") String shortName,
                                @Field("author_email") String authorEmail,
                                @Field("author_name") String authorName,
                                @Field("thread_key") String threadKey,
                                @Field("author_url") String author_url,
                                @Field("message") String message);</commitresult>

2.Field Map实现方式

[代码]java代码:

1
2
3
@FormUrlEncoded
@POST("/posts/create.json")
Call<commitresult> createCommit(@FieldMap Map<string, string=""> map);</string,></commitresult>

获取Map方式如下:

[代码]java代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class CommitParam {
  
    private String short_name;
    private String author_email;
    private String author_name;
    private String thread_id;
    private String author_url;
    private String message;
  
    public String getShort_name() {
        return short_name;
    }
  
    public void setShort_name(String short_name) {
        this.short_name = short_name;
    }
  
    public String getAuthor_email() {
        return author_email;
    }
  
    public void setAuthor_email(String author_email) {
        this.author_email = author_email;
    }
  
    public String getAuthor_name() {
        return author_name;
    }
  
    public void setAuthor_name(String author_name) {
        this.author_name = author_name;
    }
  
    public String getThread_id() {
        return thread_id;
    }
  
    public void setThread_id(String thread_id) {
        this.thread_id = thread_id;
    }
  
    public String getAuthor_url() {
        return author_url;
    }
  
    public void setAuthor_url(String author_url) {
        this.author_url = author_url;
    }
  
    public String getMessage() {
        return message;
    }
  
    public void setMessage(String message) {
        this.message = message;
    }
  
    public Map<string, string=""> createCommitParams(){
        Map<string, string=""> params = new HashMap<>();
        params.put("short_name", short_name);
        params.put("author_email", author_email);
        params.put("author_name", author_name);
        params.put("thread_id", thread_id);
        params.put("author_url", author_url);
        params.put("message", message);
        return params;
    }
}</string,></string,>

项目地址在此:Dev-Wiki/RetrofitDemo

Android Retrofit使用教程(二)的更多相关文章

  1. Android Studio系列教程二--基本设置与运行

    Android Studio系列教程二--基本设置与运行 2014 年 11 月 28 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处! 上面一篇博客,介绍了Studio的 ...

  2. Android高手进阶教程(二十八)之---Android ViewPager控件的使用(基于ViewPager的横向相册)!!!

      分类: Android高手进阶 Android基础教程 2012-09-14 18:10 29759人阅读 评论(35) 收藏 举报 android相册layoutobjectclassloade ...

  3. Android ADB命令教程二——ADB命令详解

    Android ADB命令教程二——ADB命令详解 转载▼ 原文链接:http://www.tbk.ren/article/249.html       我们使用 adb -h 来看看,adb命令里面 ...

  4. Android Studio 使用教程(二十五)之运行Android Studio工程

    一.Android虚拟设备入口 上期我们使用了Android Studio创建了HeloWorld工程,要想运行该工程,首先需要一个Android虚拟设备来模拟Android程序的运行. 重新打开An ...

  5. Android Retrofit使用教程(三):Retrofit与RxJava初相逢

    上一篇文章讲述了Retrofit的基本使用,包括GET,POST等请求.今天的文章中Retrofit要与RxJava配合使用. 了解RxJava RxJava有种种好处,我不在这里一一讲述.这里我只给 ...

  6. Android Retrofit使用教程

    Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...

  7. Android Studio系列教程四--Gradle基础

    Android Studio系列教程四--Gradle基础 2014 年 12 月 18 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://stormzhang ...

  8. 一个异常与Android Studio系列教程参考

    由于编译过程中遇到了错误:

  9. Android Studio 系列教程(转载)

    史上最详细的Android Studio系列教程一--下载和安装:http://segmentfault.com/a/1190000002401964史上最详细的Android Studio系列教程二 ...

随机推荐

  1. 分分钟教你做出自己的新闻阅读APP

    分分钟教你做出自己的新闻阅读APP 引子 曾经不小心发现了一些好的看新闻的网站,但是电脑又不是随身携带,因此想要下载一个这个网站的手机APP来看新闻,但是问题来了,这个网站根本没有做Android端! ...

  2. IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面

    前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本 ...

  3. python - 接口自动化测试 - TestLogin - 登录接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...

  4. Webapp和后端交互检查测试

    除了功能,我们可以使用下面方法,查看交互过程,页面不能发现的问题: 什么是json 什么是json,json是什么,json如何使用 JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能 ...

  5. C++指针详解 (转)

    C++指针详解 指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区, ...

  6. 一些echarts的基本图形

    先拿一个图形渲染过程举例 引用处 <bar ref="ARPUChart" v-if="ARPUChart" style="width:500p ...

  7. jQuery操作DOM基础 - 元素属性的查看与设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【bzoj4668】冷战 并查集按秩合并+朴素LCA

    题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...

  9. Redis主从复制简单介绍

    由于本地环境的使用,所以搭建一个本地的Redis集群,本篇讲解Redis主从复制集群的搭建,使用的平台是Windows,搭建的思路和Linux上基本一致! (精读阅读本篇可能花费您15分钟,略读需5分 ...

  10. xml读取 避开并发(xml的一些操作)

    很多地方读取文件可能会出现并发现象 处理: 使用FileMode.Open, FileAccess.Read, FileShare.ReadWrite 避开并发 public static List& ...