android实例教程

Welcome to Retrofit Android Example Tutorial. Today we’ll use the Retrofit library developed by Square to handle REST API calls in our android application.

欢迎使用Retrofit Android示例教程。 今天,我们将使用Square开发的Retrofit库在我们的android应用程序中处理REST API调用。

改装Android (Retrofit Android)

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. We’ll not go into the details of Retrofit 1.x versions and jump onto Retrofit 2 directly which has a lot of new features and a changed internal API compared to the previous versions.

Retrofit是适用于Android和Java的类型安全的REST客户端,旨在简化使用RESTful Web服务的过程。 我们将不介绍Retrofit 1.x版本的详细信息,而直接跳转到Retrofit 2 ,该版本具有很多新功能,并且与以前的版本相比,内部API有所更改。

Retrofit 2 by default leverages OkHttp as the networking layer and is built on top of it.

默认情况下,Retrofit 2利用OkHttp作为网络层,并在此之上构建。

Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. To serialise JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our build.grade file.

Retrofit使用POJO(普通的旧Java对象)自动序列化JSON响应,而POJO必须为JSON结构预先定义。 要序列化JSON,我们需要先将其转换为Gson的转换器。 我们需要在build.grade文件中添加以下依赖build.grade

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

OkHttp dependency is already shipped with Retrofit 2 dependency. If you wish to use a separate OkHttp dependency, you should exclude the OkHttp dependency from Retrofit 2 as:

OkHttp依赖性已经随Retrofit 2依赖性一起提供。 如果希望使用单独的OkHttp依赖项,则应将RetroHttp 2中的OkHttp依赖项排除在外:

compile ('com.squareup.retrofit2:retrofit:2.1.0') {  // exclude Retrofit’s OkHttp dependency module and define your own module importexclude module: 'okhttp'
}
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttps:3.4.1'
  • The logging-interceptor generates a log string of the entire response that’s returned.日志记录拦截器会生成返回的整个响应的日志字符串。
  • There are other converters to parse the JSON to the necessary type. A few of them are listed below.还有其他转换器可将JSON解析为必要的类型。 下面列出了其中一些。
  1. Jackson : com.squareup.retrofit2:converter-jackson:2.1.0杰克逊com.squareup.retrofit2:converter-jackson:2.1.0
  2. Moshi : com.squareup.retrofit2:converter-moshi:2.1.0Moshicom.squareup.retrofit2:converter-moshi:2.1.0
  3. Protobuf : com.squareup.retrofit2:converter-protobuf:2.1.0Protobufcom.squareup.retrofit2:converter-protobuf:2.1.0
  4. Wire : com.squareup.retrofit2:converter-wire:2.1.0连线com.squareup.retrofit2:converter-wire:2.1.0
  5. Simple XML : com.squareup.retrofit2:converter-simplexml:2.1.0简单XMLcom.squareup.retrofit2:converter-simplexml:2.1.0

Add the permission to access internet in the AndroidManifest.xml file.

AndroidManifest.xml文件中添加访问Internet的权限。

OkHttp拦截器 (OkHttp Interceptors)

Interceptors are a powerful mechanism present in OkHttp that can monitor, rewrite, and retry calls.
Interceptors can be majorly divided into two categories:

拦截器是OkHttp中存在的强大机制,可以监视,重写和重试调用。
拦截器主要可分为两类:

  • Application Interceptors : To register an application interceptor, we need to call addInterceptor() on OkHttpClient.Builder应用拦截器 :要注册应用程序拦截器,我们需要调用addInterceptor()OkHttpClient.Builder
  • Network Interceptors : To register a Network Interceptor, invoke addNetworkInterceptor() instead of addInterceptor()网络拦截器 :要注册网络拦截器,请调用addNetworkInterceptor()而不是addInterceptor()

设置改造界面 (Setting Up the Retrofit Interface)

package com.journaldev.retrofitintro;import com.journaldev.retrofitintro.pojo.MultipleResource;import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;class APIClient {private static Retrofit retrofit = null;static Retrofit getClient() {HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();retrofit = new Retrofit.Builder().baseUrl("https://reqres.in").addConverterFactory(GsonConverterFactory.create()).client(client).build();return retrofit;}}

The getClient() method in the above code will be called every time while setting up a Retrofit interface. Retrofit provides with a list of annotations for each of the HTTP methods: @GET, @POST, @PUT, @DELETE, @PATCH or @HEAD.

设置Retrofit接口时,每次都会调用上述代码中的getClient()方法。 Retrofit为每个HTTP方法提供了注释列表: @GET, @POST, @PUT, @DELETE, @PATCH or @HEAD

Let’s see how our APIInterface.java class looks like.

让我们看看我们的APIInterface.java类的样子。

package com.journaldev.retrofitintro;import com.journaldev.retrofitintro.pojo.MultipleResource;
import com.journaldev.retrofitintro.pojo.User;
import com.journaldev.retrofitintro.pojo.UserList;import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;interface APIInterface {@GET("/api/unknown")Call<MultipleResource> doGetListResources();@POST("/api/users")Call<User> createUser(@Body User user);@GET("/api/users?")Call<UserList> doGetUserList(@Query("page") String page);@FormUrlEncoded@POST("/api/users?")Call<UserList> doCreateUserWithField(@Field("name") String name, @Field("job") String job);
}

In the above class, we’ve defined some methods that perform HTTP requests with annotation.
We’ve used a few test APIs from here

在上面的类中,我们定义了一些执行带有注释的HTTP请求的方法。
我们从这里使用了一些测试API

@GET("/api/unknown") calls doGetListResources();.

@GET("/api/unknown")调用doGetListResources();

doGetListResources() is the method name. MultipleResource.java is a Model POJO class for our response object that’s used to map the response parameters to their respective variables. These POJO class act as the method return type.

doGetListResources()是方法名称。 MultipleResource.java是响应对象的Model POJO类,用于将响应参数映射到它们各自的变量。 这些POJO类充当方法的返回类型。

A simple POJO class for MultipleResources.java is given below.

下面给出了用于MultipleResources.java简单POJO类。

package com.journaldev.retrofitintro.pojo;import com.google.gson.annotations.SerializedName;import java.util.ArrayList;
import java.util.List;public class MultipleResource {@SerializedName("page")public Integer page;@SerializedName("per_page")public Integer perPage;@SerializedName("total")public Integer total;@SerializedName("total_pages")public Integer totalPages;@SerializedName("data")public List<Datum> data = null;public class Datum {@SerializedName("id")public Integer id;@SerializedName("name")public String name;@SerializedName("year")public Integer year;@SerializedName("pantone_value")public String pantoneValue;}
}

@SerializedName annotation is used to specify the name of the field that’s in the JSON Response.

@SerializedName批注用于指定JSON响应中字段的名称。

To create a POJO class for each response, we can go to https://www.jsonschema2pojo.org/ and paste the json response structure as shown in the image below.

要为每个响应创建一个POJO类,我们可以转到https://www.jsonschema2pojo.org/并粘贴json响应结构,如下图所示。

Preview the POJO class and copy it into your Android Studio Project Structure.

预览POJO类并将其复制到您的Android Studio项目结构中。

The POJO classes are wrapped into a typed Retrofit Call class.

POJO类包装在类型化的Retrofit Call类中。

Note: A JSONArray is serialised a List of Objects in the POJO classes

注意 :JSONArray已序列化POJO类中的对象列表

Method Parameters : There are a wide variety of possible options of parameters to pass inside a method:

方法参数 :有多种可能的参数选项可在方法内部传递:

  • @Body – Sends Java objects as request body.@Body –将Java对象作为请求正文发送。
  • @Url – use dynamic URLs.@Url –使用动态URL。
  • @Query – We can simply add a method parameter with @Query() and a query parameter name, describing the type. To URL encode a query use the form:
    @Query(value = "auth_token",encoded = true) String auth_token@Query –我们可以简单地使用@Query()添加一个方法参数和一个描述类型的查询参数名称。 要对查询进行URL编码,请使用以下格式:
    @Query(value = "auth_token",encoded = true) String auth_token
  • @Field – send data as form-urlencoded. This requires a @FormUrlEncoded annotation attached with the method.
    The @Field parameter works only with a POST@Field –以表单编码形式发送数据。 这需要方法附带一个@FormUrlEncoded批注。
    @Field参数仅适用于POST

Note: @Field requires a mandatory parameter. In cases when @Field is optional, we can use @Query instead and pass a null value.

注意@Field需要必填参数。 如果@Field是可选的,我们可以改用@Query并传递一个空值。

改造Android示例项目结构 (Retrofit Android Example Project Structure)

The pojo package defines four model classes for each of the API endpoint responses defined in the APIInterface.java class.

pojo包为APIInterface.java类中定义的每个API端点响应定义了四个模型类。

User.java

User.java

package com.journaldev.retrofitintro.pojo;import com.google.gson.annotations.SerializedName;public class User {@SerializedName("name")public String name;@SerializedName("job")public String job;@SerializedName("id")public String id;@SerializedName("createdAt")public String createdAt;public User(String name, String job) {this.name = name;this.job = job;}}

The above class is used to create the Response Body for the createUser() method

上面的类用于为createUser()方法创建响应主体

UserList.java

UserList.java

package com.journaldev.retrofitintro.pojo;import com.google.gson.annotations.SerializedName;import java.util.ArrayList;
import java.util.List;public class UserList {@SerializedName("page")public Integer page;@SerializedName("per_page")public Integer perPage;@SerializedName("total")public Integer total;@SerializedName("total_pages")public Integer totalPages;@SerializedName("data")public List<Datum> data = new ArrayList();public class Datum {@SerializedName("id")public Integer id;@SerializedName("first_name")public String first_name;@SerializedName("last_name")public String last_name;@SerializedName("avatar")public String avatar;}
}

CreateUserResponse.java

CreateUserResponse.java

package com.journaldev.retrofitintro.pojo;import com.google.gson.annotations.SerializedName;public class CreateUserResponse {@SerializedName("name")public String name;@SerializedName("job")public String job;@SerializedName("id")public String id;@SerializedName("createdAt")public String createdAt;
}

The MainActivity.java is where we call each of the API endpoints defined in the Interface class and display each of the fields in a Toast/TextView.

MainActivity.java中,我们调用Interface类中定义的每个API端点,并在Toast / TextView中显示每个字段。

package com.journaldev.retrofitintro;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;import com.journaldev.retrofitintro.pojo.CreateUserResponse;
import com.journaldev.retrofitintro.pojo.MultipleResource;
import com.journaldev.retrofitintro.pojo.User;
import com.journaldev.retrofitintro.pojo.UserList;import java.util.List;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;public class MainActivity extends AppCompatActivity {TextView responseText;APIInterface apiInterface;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);responseText = (TextView) findViewById(R.id.responseText);apiInterface = APIClient.getClient().create(APIInterface.class);/**GET List Resources**/Call<MultipleResource> call = apiInterface.doGetListResources();call.enqueue(new Callback<MultipleResource>() {@Overridepublic void onResponse(Call<MultipleResource> call, Response<MultipleResource> response) {Log.d("TAG",response.code()+"");String displayResponse = "";MultipleResource resource = response.body();Integer text = resource.page;Integer total = resource.total;Integer totalPages = resource.totalPages;List<MultipleResource.Datum> datumList = resource.data;displayResponse += text + " Page\n" + total + " Total\n" + totalPages + " Total Pages\n";for (MultipleResource.Datum datum : datumList) {displayResponse += datum.id + " " + datum.name + " " + datum.pantoneValue + " " + datum.year + "\n";}responseText.setText(displayResponse);}@Overridepublic void onFailure(Call<MultipleResource> call, Throwable t) {call.cancel();}});/**Create new user**/User user = new User("morpheus", "leader");Call<User> call1 = apiInterface.createUser(user);call1.enqueue(new Callback<User>() {@Overridepublic void onResponse(Call<User> call, Response<User> response) {User user1 = response.body();Toast.makeText(getApplicationContext(), user1.name + " " + user1.job + " " + user1.id + " " + user1.createdAt, Toast.LENGTH_SHORT).show();}@Overridepublic void onFailure(Call<User> call, Throwable t) {call.cancel();}});/**GET List Users**/Call<UserList> call2 = apiInterface.doGetUserList("2");call2.enqueue(new Callback<UserList>() {@Overridepublic void onResponse(Call<UserList> call, Response<UserList> response) {UserList userList = response.body();Integer text = userList.page;Integer total = userList.total;Integer totalPages = userList.totalPages;List<UserList.Datum> datumList = userList.data;Toast.makeText(getApplicationContext(), text + " page\n" + total + " total\n" + totalPages + " totalPages\n", Toast.LENGTH_SHORT).show();for (UserList.Datum datum : datumList) {Toast.makeText(getApplicationContext(), "id : " + datum.id + " name: " + datum.first_name + " " + datum.last_name + " avatar: " + datum.avatar, Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<UserList> call, Throwable t) {call.cancel();}});/**POST name and job Url encoded.**/Call<UserList> call3 = apiInterface.doCreateUserWithField("morpheus","leader");call3.enqueue(new Callback<UserList>() {@Overridepublic void onResponse(Call<UserList> call, Response<UserList> response) {UserList userList = response.body();Integer text = userList.page;Integer total = userList.total;Integer totalPages = userList.totalPages;List<UserList.Datum> datumList = userList.data;Toast.makeText(getApplicationContext(), text + " page\n" + total + " total\n" + totalPages + " totalPages\n", Toast.LENGTH_SHORT).show();for (UserList.Datum datum : datumList) {Toast.makeText(getApplicationContext(), "id : " + datum.id + " name: " + datum.first_name + " " + datum.last_name + " avatar: " + datum.avatar, Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<UserList> call, Throwable t) {call.cancel();}});}
}

apiInterface = APIClient.getClient().create(APIInterface.class); is used to instantiate the APIClient.
To map the Model class to the response we use:

apiInterface = APIClient.getClient().create(APIInterface.class); 用于实例化APIClient。
要将Model类映射到响应,我们使用:

MultipleResource resource = response.body();

MultipleResource resource = response.body();

Running the application would call each of the endpoints and display a Toast message for them accordingly.

运行该应用程序将调用每个端点,并相应地为它们显示Toast消息。

This brings an end to Retrofit android example tutorial. You can download the Android Retrofit example project from the link below.

这样就结束了Retrofit android示例教程。 您可以从下面的链接下载Android Retrofit示例项目。

Download Retrofit Android Example Project下载Retrofit Android示例项目

翻译自: https://www.journaldev.com/13639/retrofit-android-example-tutorial

android实例教程

android实例教程_改造Android示例教程相关推荐

  1. android实例教程_Android内部存储示例教程

    android实例教程 Today we will look into android internal storage. Android offers a few structured ways t ...

  2. android 导航抽屉_Android导航抽屉示例教程

    android 导航抽屉 In this tutorial we'll implement a Navigation Drawer in our android application. Androi ...

  3. android浮动按钮_Android浮动操作按钮示例教程

    android浮动按钮 Today we will learn about Android Floating Action Button. We'll discuss the FloatingActi ...

  4. android圆角视图_Android图库视图示例教程

    android圆角视图 Android Gallery is a View commonly used to display items in a horizontally scrolling lis ...

  5. android edittext不可复制_精选Android中高级面试题:性能优化,JNI,设计模式

    性能优化 1.图片的三级缓存中,图片加载到内存中,如果内存快爆了,会发生什么?怎么处理? 参考回答:首先我们要清楚图片的三级缓存是如何的: 如果内存足够时不回收.内存不够时就回收软引用对象 2.内存中 ...

  6. 计算机系统精品教程,_《计算机操作系统教程》课程教学课件(精品) 设备管理(88P).pdf...

    _<计算机操作系统教程>课程教学课件(精品) 设备管理(88P).pdf 第8章 设备管理 §8.1 引言 §8.2 数据传送控制方式 §8.3 中断技术 §8.4 缓冲技术 §8.5 设 ...

  7. [Android实例] 最全的Android开发资源整理--进阶必备

    本帖最后由 一切随枫 于 2014-6-9 12:08 编辑 原文链接: http://stormzhang.github.io/android/2014/06/05/android-awesome- ...

  8. Word教程_编程入门自学教程_菜鸟教程-免费教程分享

    教程简介 Word 2010入门教程 - 从简单的步骤入门Microsoft Office 2010,从基本到高级概念,包括探索窗口,后台视图,输入文本,移动,打开,关闭文档,上下文帮助,插入,选择, ...

  9. Apache POI(Word)教程_编程入门自学教程_菜鸟教程-免费教程分享

    教程简介 IT宝库整理的Apache POI Word入门教程 - 从基本到高级概念的简单简单步骤学习Apache POI Word,其中包括概述,Apache POI安装,核心类,文档,段落,边框, ...

最新文章

  1. mysql ibd文件还原_Mysql 通过ibd文件恢复数据
  2. python装饰器函数-Python函数装饰器常见使用方法实例详解
  3. 穷学计算机富学金融家里有矿,穷学IT,富学金融?亲身体验告诉你IT真的是一个不用拼爹的行业...
  4. oracle ORA-00604和BadImageFormatException的解决方法
  5. 微软在Build 2019大会上发布Fluid Framework协作平台
  6. Leet Code OJ 7. Reverse Integer [Difficulty: Easy]
  7. Java 单例模式:懒加载(延迟加载)和即时加载
  8. JS执行Promise
  9. 定期定量采购_采购的四种方法
  10. 【数据结构笔记39】哈希表/散列表、(数据关键字/字符串关键字)散列构造函数
  11. Xml解析之PULL解析 例2
  12. 知识图谱、Gremlin Traversal Language、RDF、Amazon Nepture 图数据库介绍
  13. vivado SRIO 学习
  14. 【动态规划】 EditDistance
  15. 记:COFF文件下的解析说明
  16. mac系统安装搭载Windows系统虚拟机方法教程
  17. 什么是模型?开发软件为什么要建模?
  18. 微信开放平台开发第三方授权登陆:微信扫码登录
  19. 【3D视觉原理】2-3D传感器原理
  20. JS中常见的字符串拼接处理

热门文章

  1. 基于Volley框架的图片渐变显示alpha动画
  2. 关于今日头条小程序(字节跳动小程序)相关问题
  3. 防风网-挡风抑尘墙:挡风抑尘墙
  4. 软工实践第八次作业(软件工程实践总结)
  5. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II
  6. 重点推荐:HP大中华区总裁孙振耀退休感言
  7. Chapter 1: 使用引用类型
  8. pytorch---之cudnn.benchmark和cudnn.deterministic
  9. (二)设置hexo支持mermaid
  10. LeetCode-7.整数反转(越界问题)