搜索
您的当前位置:首页正文

Retrofit(一)

来源:二三娱乐

介绍了Retrofit简单使用

新建项目
添加权限

<uses-permission android:name="android.permission.INTERNET"/>

添加依赖

compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.3.0'  //String解析器
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'  //gson解析器

1.Retrofit的简单使用

新建一个接口HttpService:

public interface HttpService {
    
    @GET("top250")
    Call<DouBanBean> getTop(@Query("start") int start, @Query("count") int count);
    
    @FormUrlEncoded
    @POST("postBody")
    Call<String> getUpload(@Field("aaa") String aaa, @Field("bbb") String bbb);
}

使用

  • GET请求
//请求接口 
private void netGet() {
        Retrofit retrofit = new Retrofit.Builder()
                
                .addConverterFactory(GsonConverterFactory.create())  //转换器
                .build();

        HttpService httpService = retrofit.create(HttpService.class);

        Call<DouBanBean> top = httpService.getTop(0, 2);
        
        top.enqueue(new Callback<DouBanBean>() {
            @Override
            public void onResponse(Call<DouBanBean> call, Response<DouBanBean> response) {
                Zx.d(response.toString());
                Zx.d(response.headers());
                DouBanBean douBanBean = response.body();
            }

            @Override
            public void onFailure(Call<DouBanBean> call, Throwable t) {
                Zx.d(t.getMessage());
            }
        });
        
    }
  • POST请求
//请求接口    aaa=111  bbb=222
private void netPost() {
        Retrofit retrofit = new Retrofit.Builder()
                  //地址
                .addConverterFactory(ScalarsConverterFactory.create())  //转换器
                .build();

        HttpService httpService = retrofit.create(HttpService.class);

        Call<String> upload = httpService.getUpload("111", "222");
        upload.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Zx.d(response.toString());
                Zx.d(response.headers());
                Zx.d(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Zx.d(t.getMessage());
            }
        });
    }

over

其中@GET @POST指定了请求方法,Retrofit框架提供了很多注解,其中HTTP注解有五个

@GET
@POST
@PUT
@DELETE
@HEAD

GET

  • @Path 替换参数
@GET("group/{id}/users")
List<User> groupList(@Path("id") int groupId);

这里使用了传入的groupId替换了"group/{id}/users"中的{id}

  • @Query 查询参数
 @GET("login")
List<User> httpLogin(@Query ("name") String name,@Query ("password") String password);

这里相当于login?name=xxx&password=xxx

  • 同时存在的情况 可在后面继续添加@Path或@Query,或将多个查询参数放入Map中
@GET("group/{id}/users")
List<User> groupList(@Path("id") int groupId,@Query ("name") String name,@Query("password") String password);
//或
@GET("group/{id}/users")
List<User> groupList(@Path("id") int groupId,@QueryMap Map<String, String> options);

POST

  • 提交表单数据
    @FormUrlEncoded
    @POST("postBody")
    Call<String> getLogin(@Field("name") String name, @Field("password") String password);

使用 @FormUrlEncoded 修饰请求方法,可以把表单数据提交到服务端
用 @Field 修饰key-value对

上传文件

使用 @Multipart 修饰请求方法

    @Multipart
    @POST("images")
    Call<String> upload(@Part("description") RequestBody description, @Part MultipartBody.Part file);
    private void netUpload() {
        Retrofit retrofit = new Retrofit.Builder()
                
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        HttpService httpService = retrofit.create(HttpService.class);

        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), "aaa");
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), new File(""));
        MultipartBody.Part part = MultipartBody.Part.createFormData("pototo", "", fileBody);
        
        httpService.upload(requestBody, part);
    }
Top