Android-retrofit-基础

http://daidingkang.cc/2016/06/17/Retrofit2-network-framework-parsing/

http://blog.csdn.net/liuhongwei123888/article/details/50375283


前言:

  纪念这个明天比昨天更好的时代,纪念这个二胎开放的时代,对于技术日新月累的今天,各种新的网络技术也层数不穷,从老牌的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,怎么样,是不是很简单呢。

下面我贴出完整的代码,主要是两个类RequestSerivesMainActivity


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")的参数不宜过长





例子


编写API服务代码

     

[java]  view plain  copy
  1. package com.micky.retrofitrxandroiddagger2.data.api;  
  2.   
  3. import com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse;  
  4.   
  5. import retrofit.Call;  
  6. import retrofit.http.GET;  
  7. import retrofit.http.Query;  
  8. import rx.Observable;  
  9.   
  10. /** 
  11.  * @Project retrofitrxandroiddagger2 
  12.  * @Packate com.micky.retrofitrxandroiddagger2.data.api 
  13.  * @Description 
  14.  * @Author Micky Liu 
  15.  * @Email mickyliu@126.com 
  16.  * @Date 2015-12-21 17:22 
  17.  * @Version 1.0 
  18.  */  
  19. public interface ApiService {  
  20.     @GET("service/getIpInfo.php")  
  21.     Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);  
  22. }  

3、定义接收数据的response

[java]  view plain  copy
  1. package com.micky.retrofitrxandroiddagger2.data.api.response;  
  2.   
  3.   
  4. import com.micky.retrofitrxandroiddagger2.data.api.model.IpInfo;  
  5.   
  6. /** 
  7.  * @Project CommonProject 
  8.  * @Packate com.micky.commonproject.data.api.response 
  9.  * @Description 
  10.  * @Author Micky Liu 
  11.  * @Email mickyliu@126.com 
  12.  * @Date 2015-12-21 16:39 
  13.  * @Version 1.0 
  14.  */  
  15. public class GetIpInfoResponse extends BaseResponse {  
  16.     public IpInfo data;  
  17. }  

4、请求数据并显示

[java]  view plain  copy
  1. package com.micky.retrofitrxandroiddagger2;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.design.widget.FloatingActionButton;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.support.v7.widget.Toolbar;  
  7. import android.view.View;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.widget.ProgressBar;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13.   
  14. import com.micky.retrofitrxandroiddagger2.common.utils.CrashHandler;  
  15. import com.micky.retrofitrxandroiddagger2.data.api.ApiService;  
  16. import com.micky.retrofitrxandroiddagger2.data.api.response.GetIpInfoResponse;  
  17.   
  18. import retrofit.Call;  
  19. import retrofit.Callback;  
  20. import retrofit.GsonConverterFactory;  
  21. import retrofit.Response;  
  22. import retrofit.Retrofit;  
  23. import retrofit.RxJavaCallAdapterFactory;  
  24. import rx.Subscriber;  
  25. import rx.android.schedulers.AndroidSchedulers;  
  26. import rx.functions.Action1;  
  27. import rx.schedulers.Schedulers;  
  28.   
  29. /** 
  30.  * @Project retrofitrxandroiddagger2 
  31.  * @Packate com.micky.retrofitrxandroiddagger2 
  32.  * @Description 
  33.  * @Author Micky Liu 
  34.  * @Email mickyliu@126.com 
  35.  * @Date 2015-12-21 17:35 
  36.  * @Version 1.0 
  37.  */  
  38. public class MainActivity extends AppCompatActivity {  
  39.   
  40.     private static final String ENDPOINT = "http://ip.taobao.com";  
  41.     private TextView mTvContent;  
  42.     private ProgressBar mProgressBar;  
  43.   
  44.     @Override  
  45.     protected void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         setContentView(R.layout.activity_main);  
  48.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  49.         setSupportActionBar(toolbar);  
  50.         mTvContent = (TextView) findViewById(R.id.tv_content);  
  51.         mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);  
  52.   
  53.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
  54.         fab.setOnClickListener(new View.OnClickListener() {  
  55.             @Override  
  56.             public void onClick(View view) {  
  57.   
  58.                 Retrofit retrofit = new Retrofit.Builder()  
  59.                         .baseUrl(ENDPOINT)  
  60.                         .addConverterFactory(GsonConverterFactory.create())  
  61.                         .build();  
  62.                 ApiService apiService = retrofit.create(ApiService.class);  
  63.   
  64.                 mProgressBar.setVisibility(View.VISIBLE);  
  65.   
  66.                 Call<GetIpInfoResponse> call = apiService.getIpInfo("63.223.108.42");  
  67.                 call.enqueue(new Callback<GetIpInfoResponse>() {  
  68.                     @Override  
  69.                     public void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) {  
  70.                         mProgressBar.setVisibility(View.GONE);  
  71.                         GetIpInfoResponse getIpInfoResponse = response.body();  
  72.                         mTvContent.setText(getIpInfoResponse.data.country);  
  73.                     }  
  74.   
  75.                     @Override  
  76.                     public void onFailure(Throwable t) {  
  77.                         mProgressBar.setVisibility(View.GONE);  
  78.                         mTvContent.setText(t.getMessage());  
  79.                     }  
  80.                 });  
  81.             }  
  82.         });  
  83.     }  
  84.   
  85.     @Override  
  86.     public boolean onCreateOptionsMenu(Menu menu) {  
  87.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  88.         return true;  
  89.     }  
  90.   
  91.     @Override  
  92.     public boolean onOptionsItemSelected(MenuItem item) {  
  93.         int id = item.getItemId();  
  94.         if (id == R.id.action_settings) {  
  95.             return true;  
  96.         }  
  97.   
  98.         return super.onOptionsItemSelected(item);  
  99.     }  
  100. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值