全新的网络加载框架Retrofit2,上位的小三

xiaoxiao2021-02-28  34

前言:

  纪念这个明天比昨天更好的时代,纪念这个二胎开放的时代,对于技术日新月累的今天,各种新的网络技术也层数不穷,从老牌的HttpClient,那时还不能称之为框架;到android-async-http,再到如今的老将Volley;曾几何时,正是Xutils,Afindl等大而全的框架盛行其道的时候,如今也已经褪去了往日的辉煌,留下了无尽的悔恨暗暗孤独终老;如今新的网络框架诸如Okhttp,Retrofit等异军突起,新技术新思想如雨后春笋般冒了出来,设计模式MVP,MVVM蒸蒸日上,Google都已经抛弃了apache,我们还有什么理由不跟紧时代;Android都已经是N了,谁还为14以下做兼容,AS都已经2.1了谁还死守着Eclipse;对于变化如此快的世界,我不会抱怨也不会有欣喜;默默的望着灰色的屏幕,继续敲着我未完成的代码…

主题

  来自Retrofit的官网介绍

A type-safe HTTP client for Android and Java 翻译:Android和Java的类型安全的HTTP客户端

  首先简单介绍下Retrofit这个框架,Retrofit是底层是基于Okhttp的也就是说用法和Okhttp很相似;Retrofit它是一个HTTP请求工具,和Google开发的Volley功能上非常相似,这里有Volley的源码解析,但是使用上很不相似。Retrofit使用起来更简单,Volley使用上更加原始而且符合使用者的直觉,其实我觉得如果对自己Volley封装一下也可以像Retrofit那样的简单的使用;

如何使用Retrofit

  第一步当然是导包了,需要导入的包如下:

compile 'io.reactivex:rxjava:1.1.0'//Rx的包 compile 'io.reactivex:rxandroid:1.1.0'//Rx的包 compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包 compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'//CallAdapterFactory的Rx依赖包 compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包 compile 'com.google.code.gson:gson:2.6.2'//Gosn解析包

这里需要值得注意的是导入的retrofit2包的版本必须要一致,否则就会报错,我跳过的坑大家就不要再跳了

  首先定义我们请求的Api,我们假设是这样的

http://106.3.227.33/pulamsi/mobileLogin/submit.html

与Okhttp不同的是,Retrofit需要定义一个接口,用来返回我们的Call对象,这里示范的是Post请求:

1 2 3 4 5 6 public interface RequestSerives { @POST("mobileLogin/submit.html") @FormUrlEncoded Call<String> getString(@Field("loginname") String loginname, @Field("nloginpwd") String nloginpwd); }

细心的同学可能注意到了,这段代码用了两个注解,我们一一来解答;

Retrofit提供的请求方式注解有@GET和@POST,参数注解有@PATH和@Field等,我们只介绍常用的;前两个顾名思义就是定义你的请求方式Get or Post,后面的@PATH指的是通过参数填充完整的路径,一般用法:

1 2 @GET("{name}") Call<User> getUser(@Path("name") String name);

这里的参数username会被填充至{name}中,形成完整的Url请求地址,{name}相当于一个占位符;

@Query就是我们的请求的键值对的设置,我们构建Call对象的时候会传入此参数, @FormUrlEncoded

1 2 3 4 @POST("mobileLogin/submit.html") @FormUrlEncoded Call<String> getString(@Field("loginname") String loginname, @Field("nloginpwd") String nloginpwd);

ok,看起来也很简单,通过@POST指明url,添加@FormUrlEncoded,然后通过@Field添加参数即可。 这里@Field("loginname")就是键,后面的loginname就是具体的值了,值得注意的是Get和Post请求,都是这样填充参数的;

接口写完了之后我们需要来定义Retrofit对象来进行请求了;

创建一个Retrofit 对象

1 2 3 4 5 6 7 8 9 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://106.3.227.33/pulamsi/") //增加返回值为String的支持 .addConverterFactory(ScalarsConverterFactory.create()) //增加返回值为Gson的支持(以实体类返回) .addConverterFactory(GsonConverterFactory.create()) //增加返回值为Oservable<T>的支持 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build();

这里的baseurl加上之前@POST("mobileLogin/submit.html")定义的参数形成完整的请求地址;

addConverterFactory(ScalarsConverterFactory.create())的意思是构建了一个返回支持,如果你的Call对象的泛型接收另外的格式需要添加另外的支持,上述代码已经列出;

接下来我们用这个Retrofit对象创建一个RequestSerives接口对象,也就是我们之前定义的那个接口,并且得到我们的Call对象;

1 2 RequestSerives requestSerives = retrofit.create(RequestSerives.class);//这里采用的是Java的动态代理模式 Call<String> call = requestSerives.getString("userName", "1234");//传入我们请求的键值对的值

利用得到的Call对象,然后我们就发出网络请求了:

1 2 3 4 5 6 7 8 9 10 11 call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.e("成功",response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { Log.e("失败","失败"); } });

请求成功会调用onResponse方法,并且请求成功的值在response.body()中,失败则会调用onFailure,怎么样,是不是很简单呢。

下面我贴出完整的代码,主要是两个类RequestSerives和MainActivity;


RequestSerives.class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import retrofit2.Call; import retrofit2.http.POST; import retrofit2.http.Query; /** * User: Daidingkang(ddk19941017@Gmail.com) * Date: 2016-06-15 * Time: 11:22 * FIXME */ public interface RequestSerives { @POST("mobileLogin/submit.html") @FormUrlEncoded Call<String> getString(@Field("loginname") String loginname, @Field("nloginpwd") String nloginpwd); }

MainActivity.class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://106.3.227.33/pulamsi/") .addConverterFactory(ScalarsConverterFactory.create()) .build(); RequestSerives requestSerives = retrofit.create(RequestSerives.class); Call<String> call = requestSerives.getString("13288453023", "654"); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.e("成功",response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { Log.e("失败","失败"); } }); } }

Retrofit的图片与多图片上传(坑)

后台使用Spring MVC

单张图片

先看接口定义,传递文件必须加上@Multipart 类型为MultipartBody.Part

1 2 3 4 //单张图片上传 @Multipart @POST("addPaster.html") Call<String> updateImage(@Part MultipartBody.Part file);

上传准备Part

1 2 3 4 5 6 7 8 // 1、根据地址拿到File File file = new File(path); // 2、创建RequestBody,其中`multipart/form-data`为编码类型 RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); // 3、创建`MultipartBody.Part`,其中需要注意第一个参数`fileUpload`需要与服务器对应,也就是`键` MultipartBody.Part part = MultipartBody.Part.createFormData("fileUpload", file.getName(), requestFile);

多张图片

多张接口定义,这次为数组

1 2 3 4 //多张图片上传 @Multipart @POST("addPaster.html") Call<String> updateImage(@Part MultipartBody.Part[] file);

图文

1 2 3 4 //图文上传 @Multipart @POST("android/paster/addPaster.html") Call<DefaultResult> updateImage(@Part MultipartBody.Part[] parts,@QueryMap Map<String, String> maps);

上传准备Part

1 2 3 4 5 6 7 8 9 10 // 1、根据地址拿到File File file = new File(path); // 2、创建RequestBody,其中`multipart/form-data`为编码类型 RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); // 3、创建`MultipartBody.Part`数组,多图片上传,其中需要注意第一个参数`fileUpload`需要与服务器对应,也就是`键`,多个也一样 MultipartBody.Part[] file = new MultipartBody.Part[2]; file[0] = MultipartBody.Part.createFormData("file", file1.getName(), requestFile); file[1] = MultipartBody.Part.createFormData("file", file1.getName(), requestFile);

准备提交

1 2 3 4 5 6 7 8 9 10 11 12 API.Retrofit().updateImage(file).enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Toast.makeText(MainActivity.this, response.message(), Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, response.code() + "", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<String> call, Throwable t) { Toast.makeText(MainActivity.this, t.toString(), Toast.LENGTH_SHORT).show(); } });

完整代码

小小的封装了一下

API.class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class API { private static RetrofitAPI retrofitAPI; public static RetrofitAPI Retrofit() { if (retrofitAPI == null) { retrofitAPI = new Retrofit.Builder() .baseUrl("http://192.168.1.223:8080/pulamsi/android/paster/") .addConverterFactory(ScalarsConverterFactory.create()) .build() .create(RetrofitAPI.class); } return retrofitAPI; } public interface RetrofitAPI { //单张图片上传 @Multipart @POST("addPaster.html") Call<String> updateImage(@Part MultipartBody.Part file); //多张图片上传 @Multipart @POST("addPaster.html") Call<String> updateImage(@Part MultipartBody.Part[] file); } }

MainActivity.class

1 2 3 4 5 6 7 8 9 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 public class MainActivity extends AppCompatActivity { private File file1, file2; private String path1, path2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { retrofit(); } }); } private void initView() { path1 = "/storage/emulated/0/BGAPhotoPickerDownload/7ee30eacf00760ea0cbffccfa7f1b400.png"; path2 = "/storage/emulated/0/BGAPhotoPickerDownload/c45c2dd688f90771680d323431e3452f.png"; file1 = new File(path1); file2 = new File(path2); } //网络请求 private void retrofit() { // 1、创建RequestBody RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file1); // 2、创建MultipartBody.Part,其中需要注意第一个参数需要与服务器对应 MultipartBody.Part part = MultipartBody.Part.createFormData("file", file1.getName(), requestFile); MultipartBody.Part[] file = new MultipartBody.Part[2]; file[0] = MultipartBody.Part.createFormData("file", file1.getName(), requestFile); file[1] = MultipartBody.Part.createFormData("file", file1.getName(), requestFile); // 3、调用uploadImage上传图片 API.Retrofit().updateImage(file).enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Toast.makeText(MainActivity.this, response.message(), Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, response.code() + "", Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<String> call, Throwable t) { Toast.makeText(MainActivity.this, t.toString(), Toast.LENGTH_SHORT).show(); } }); } }

Spring服务端接收代码

这种方式是需要两个文件流的,单独传一个会报错,也是我改了很久的原因,一般情况,接收只需一个参数即可

1 2 3 4 5 6 7 8 9 // 新增 @RequestMapping(value = "addPaster", method = RequestMethod.POST) // filed多图接收做九图图集,file1单图接收,做主图 public void addPaster(@RequestParam MultipartFile[] file, @RequestParam MultipartFile file1, HttpServletResponse response, HttpServletRequest request) throws IllegalStateException, IOException, ParseException { //省略保存代码 。。。。。。。。 }

笔记

之前传递参数的时候会报几种错误代码

code 400 Bad Request 这种情况一般都是参数类型与服务器不匹配,一般需要检查参数和服务器参数是否对应的上

code 500 Internal server error 在确认你的Url没有错误的话,一般都是服务器的原因

code 404 Not found 请检查url是否拼接正确,一般来说都@POST("addPaster.html")的参数不宜过长

扩展阅读 Retrofit2 完全解析 探索与okhttp之间的关系 Retrofit2 完全解析 探索与okhttp之间的关系 - 张鸿阳

框架拓展

RxJava 与 Retrofit 结合的最佳实践

转载请注明原文地址: https://www.6miu.com/read-1000313.html

最新回复(0)