简介
Retrofit是Square公司开发的一款针对Android网络请求的框架,官网地址http://square.github.io/retrofit/ ,在官网上有这样的一句话介绍retrofit,A type-safe HTTP client for Android and Java。我们知道Retrofit底层是基于OKHttp实现的。对okhttp不了解的同学可以看看这一篇文章。okhttp源码解析https://www.cnblogs.com/huangjialin/p/9469373.html
okhttp简单使用
build.gradle依赖
11 implementation 'com.squareup.retrofit2:retrofit:2.4.0' 22 implementation 'com.squareup.retrofit2:converter-scalars:2.4.0' 33 implementation ‘com.squareup.retrofit2:converter-gson:2.4.0'
创建请求接口
1 1 /** 2 2 * Created by huangjialin on 2018/8/16. 3 3 */ 4 4 5 5 public interface ApiService { 6 6 7 7 @GET("api//Client/GetIsOpen") 8 8 Call<SwitchTest> getInformation(); 9 9 1010 }
这里以GET请求为例,POST请求后面还会提。接口只是为了测试用,返回什么数据,不要在意。接下来看配置Retrofit
1 1 package com.example.huangjialin.retrofittest; 2 2 3 3 import android.os.Bundle; 4 4 import android.support.v7.app.AppCompatActivity; 5 5 import android.util.Log; 6 6 import android.view.View; 7 7 8 8 import retrofit2.Call; 9 9 import retrofit2.Callback; 1010 import retrofit2.Response; 1111 import retrofit2.Retrofit; 1212 import retrofit2.converter.gson.GsonConverterFactory; 1313 import retrofit2.converter.scalars.ScalarsConverterFactory; 1414 1515 public class MainActivity extends AppCompatActivity { 1616 1717 @Override 1818 protected void onCreate(Bundle savedInstanceState) { 1919 super.onCreate(savedInstanceState); 2020 setContentView(R.layout.activity_main); 2121 findViewById(R.id.test).setOnClickListener(new View.OnClickListener() { 2222 @Override 2323 public void onClick(View view) { 2424 testRetrofit(); 2525 } 2626 }); 2727 } 2828 2929 3030 String url = "http://10.31.1.2:93"; 3131 3232 public void testRetrofit() { 3333 //创建retrofit 3434 Retrofit retrofit = new Retrofit.Builder() 3535 .baseUrl(url) //设置baseURl 3636 .addConverterFactory(ScalarsConverterFactory.create()) //增加返回值为String的支持 3737 .addConverterFactory(GsonConverterFactory.create()) 3838 .build(); 3939 ApiService apiService = retrofit.create(ApiService.class); 4040 Call<SwitchTest> call = apiService.getInformation(); 4141 call.enqueue(new Callback<SwitchTest>() { 4242 @Override 4343 public void onResponse(Call<SwitchTest> call, Response<SwitchTest> response) { 4444 Log.d("huangjialin", "---onResponse----" + response.body().getMessage()); 4545 } 4646 4747 @Override 4848 public void onFailure(Call<SwitchTest> call, Throwable t) { 4949 Log.d("huangjialin", "--- 失败----" ); 5050 } 5151 }); 5252 } 5353 }
Model类SwitchTest
1 1 package com.example.huangjialin.retrofittest; 2 2 3 3 /** 4 4 * Created by huangjialin on 2018/8/16. 5 5 */ 6 6 7 7 class SwitchTest { 8 8 private String Message; 9 9 private ModelBean Model; 1010 private int ResultType; 1111 1212 public String getMessage() { 1313 return Message; 1414 } 1515 1616 public void setMessage(String message) { 1717 Message = message; 1818 } 1919 2020 public ModelBean getModel() { 2121 return Model; 2222 } 2323 2424 public void setModel(ModelBean model) { 2525 Model = model; 2626 } 2727 2828 public int getResultType() { 2929 return ResultType; 3030 } 3131 3232 public void setResultType(int resultType) { 3333 ResultType = resultType; 3434 } 3535 3636 public static class ModelBean { 3737 private boolean Phone; 3838 private boolean Message; 3939 private boolean ClientApp; 4040 4141 public boolean isPhone() { 4242 return Phone; 4343 } 4444 4545 public void setPhone(boolean phone) { 4646 Phone = phone; 4747 } 4848 4949 public boolean isMessage() { 5050 return Message; 5151 } 5252 5353 public void setMessage(boolean message) { 5454 Message = message; 5555 } 5656 5757 public boolean isClientApp() { 5858 return ClientApp; 5959 } 6060 6161 public void setClientApp(boolean clientApp) { 6262 ClientApp = clientApp; 6363 } 6464 } 6565 6666 }
最后记得加上网络权限
1 <uses-permission android:name=“android.permission.INTERNET"/>
retrofit一个简单的网络请求就可以了……当然,我这里的url是用我司的测试地址,是有网络权限的,所以记得替换成你们自己的地址。使用比较简单,这里不做过多的解释。下面我们看看retrofit的注解
请求方法
http请求方式一共有7种,分别是GET,POST、PUT、DELETE、HEAD、OPTIONS、PATCH,而其中我们最常用的就是get和post请求,这里也主要拿这两种方式分析。retrofit设置请求方法主要是通过注解的方式
get请求
11 @GET("api//Client/GetIsOpen") 22 Call<SwitchTest> getInformation();
post请求
11 @POST("api/login/GetModel") 22 Call<LoginModel> login(@Body Login login);
只需要在每个请求接口上方加上@GET(“aaaa”)或者是@POST(“aaaa”)就可以设置不同请求方式。retrofit除了可以通过注解的方式设置请求方法外,还可以通过注解来设置请求参数。
GET网络请求注解参数方式
@Query
11 @GET("api//Client/GetIsOpen") 22 Call<SwitchTest> getInformation(@Query("id") String id,@Query("page") int page);
这种方式会将这些请求参数附加到URL后面,也就是拼接起来。如上面设置的,最后会成为api//Client/GetIsOpen/id/page,当然,如果参数很多,可以使用下面这种方式
@QueryMap
11 @GET("api//Client/GetIsOpen") 22 Call<SwitchTest> getInformation(@QueryMap Map<String,String> parame);
这种方式可以将参数全部存放到Map中,源码中是这样解释QueryMap的Query parameter keys and values appended to the URL.意思是会将这个key值拼接到url上面。
除了可以拼接参数以外,还可以动态的拼接URL地址,使用@Path
@Path
源码这样介绍Named replacement in a URL path segment. Values are converted to strings using
11 @GET("api//Client/{path}") 22 Call<SwitchTest> pathTest(@Path("path")String path);
这样我们在调用这个接口的时候,传入的参数就会替换这个path。
POST网络请求注解参数方式
我们知道,post请求的参数不是以拼接的形式的,而是将参数放到请求体中的,所以retrofit注解post请求的参数是以另外的方式
@Body
源码是这样解释的Use this annotation on a service method param when you want to directly control the request body,意思是当您想要直接控制请求体时,在服务方法参数上使用此注释
11 @POST("api/login/GetModel") 22 Call<LoginModel> login(@Body Login login);
其中Login是一个实体类,上面这个接口api/login/GetModel,是一个登陆接口,登陆就需要账号密码,所以这个实体类就可以这样设置。
1 1 package com.example.huangjialin.retrofittest; 2 2 3 3 /** 4 4 * Created by huangjialin on 2018/8/16. 5 5 */ 6 6 7 7 public class Login { 8 8 private String tel; 9 9 private String password; 1010 1111 public Login(String tel, String password) { 1212 this.tel = tel; 1313 this.password = password; 1414 } 1515 }
当我们调用这个接口时,就可以传入new Login(“账号”,”密码”);
@Field
源码是这样解释的:Named pair for a form-encoded request.
11 @FormUrlEncoded 22 @POST("api/login/GetModel") 33 Call<LoginModel> login(@Field("tel") String tel, 44 @Field("password") String password);
这里需要注意一个地方使用@Field的时候,一定要记得加上@FormUrlEncoded,这个作用是表示请求主体将使用表单URL编码。
@FieldMap
源码是这样解释的:Named key/value pairs for a form-encoded request.翻译就是:表单编码请求的命名键/值对。简单一点的理解就是这样FieldMap可以将参数全部存放到map中。
11 @FormUrlEncoded 22 @POST("api/login/GetModel") 33 Call<LoginModel> login(@FieldMap Map<String,String>parame);
@Part
源码是这样解释的:Denotes a single part of a multi-part request.
11 @Multipart 22 @POST("user/photo") 33 Call<实体类> updateUser(@Part MultipartBody.Part photo, @Part("description") RequestBody description);
其中使用@Part 一定要记得加上@Multipart 源码是这样解释这个的Denotes that the request body is multi-part. Parts should be declared as parameters and annotated with {@link Part @Part}. 表示请求主体是多部分的。部件应该声明为参数和 注释为{@link Part @Part}。当然使用@Part 主要是用来上传文件的,并且是单文件上传的。如果多文件上传的话,可以使用下面这个
@PartMap
源码是这样解释的:Denotes name and value parts of a multi-part request.
11 @Multipart 22 @POST("user/photo") 33 Call<实体类> updateUser(@PartMap Map<String, RequestBody> photos, 44 @Part("description") RequestBody description);
除了上面这些注解外,我们往往还能看到这个注解
@Headers
11 @Headers("Cache-Control: max-age=640000") 22 @POST("api/login/GetModel") 33 Call<LoginModel> login(@Body Login login);
源码的解释和例子是这样的
1 1 /** 2 2 * Adds headers literally supplied in the {@code value}. 3 3 * <pre><code> 4 4 * @Headers("Cache-Control: max-age=640000") 5 5 * @GET("/") 6 6 * ... 7 7 * 8 8 * @Headers({ 9 9 * "X-Foo: Bar", 1010 * "X-Ping: Pong" 1111 * }) 1212 * @GET("/") 1313 * ... 1414 * </code></pre> 1515 * <strong>Note:</strong> Headers do not overwrite each other. All headers with the same name will 1616 * be included in the request. 1717 * 1818 * @see Header 1919 * @see HeaderMap 2020 */
有什么用的呢??在http请求中,为了防止攻击或是过滤掉不安全的访问或是添加特殊加密的访问等等,用来减轻服务器的压力和保证请求的安全等,就会在请求头上面加一些内容。
本来,想弄成一篇文章的,但是写完到这里以后,感觉篇幅好长,所以在这里分一下篇幅吧
Retrofit源码解析(下)https://www.cnblogs.com/huangjialin/p/9492271.html