Retrofit基本使用方法

Retrofit github地址:https://github.com/square/retrofit

            官方文档:http://square.github.io/retrofit/


接口文档:http://www.sojson.com/api/weather.html

使用的接口:http://www.sojson.com/open/api/weather/json.shtml?city=深圳


1.新建一个Retrofit对象

   

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.sojson.com/")
                .build();
      baseUrl这里只是填入到了域名部分,必须以"/"结尾


2.新建一个接口

   

public interface RequestService {

    @GET("open/api/weather/json.shtml")
    Call<ResponseBody> getWeather(@Query("city") String cityName);
}
      这里用的是GET请求方式,Retrofit支持 GET POST PUT DELETE , 和  HEAD

  "open/api/weather/json.shtml"这个是链接的路径  

  @Query("city") 这里通过注解的方式,加入city请求参数


3.新建一个接口代理对象

   

RequestService requestSerives = retrofit.create(RequestService.class);

4.通过Call对象,把请求插入请求队列

 

Call<ResponseBody> call = requestSerives.getWeather("深圳");//传入我们请求的键值对的值
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.d("MainActivity","return:" + response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                Log.d("MainActivity","error");
            }
        });

5.url的传参方式

 (1)@Path 传入路径中的参数

        

//路径替换
    @GET("open/api/{action}/json.shtml")
    Call<String> getStringByAction(@Path("action") String action, @Query("city") String cityName);

   这里路径中我们加多了{action}(注意必须要用“{}”),再在传递的参数中加多了@Path("action"),参数名action要一致,以此来动态修改我们的请求地址的路径

  

Call<String> call2 = requestSerives.getStringByAction("weather", "深圳");
        call2.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d("MainActivity","return2:" + response.body().toString());
                Log.d("MainActivity", "Thread Name :" + Thread.currentThread().getName());
            }

            @Override
            public void onFailure(Call<String> call, Throwable throwable) {
                Log.d("MainActivity","error");
            }
        });

 (2)@Query 传递参数

  这个前面已经有例子,如果需要传递多组参数,对应的有@QueryMap

  

@GET("open/api/weather/json.shtml")
    Call<String> getWeather(@QueryMap Map<String, String> params);
   @Query @QueryMap都是直接加载url后面的参数,因此这种方式适合GET请求方式

  如果是POST请求,参数是在请求头中的,则需要使用@Field @FieldMap

   

    (3)@Body 传递对象

      

@POST("users/new")
Call<User> createUser(@Body User user)

  使用于POST请求,整个对象会转换为请求体

   

    (4)@Part        

     需要和@Multipart配合使用,适合于表单数据上传 类似还有@PartMap

     

6.加入请求头

 

@Headers({
    "Accept: application/vnd.github.v3.full+json",
    "User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值