retrofit的简单使用(不依赖rxjava)

项目一直在用okgo,新项目打算使用retrofit,看了一下,自己记录一下基本使用。。。1.首先单例一个Retrofitpublic class RetrofitUtils{ public static Retrofit retrofit; public static final String END_POINT = "xxxxx"; public static Retrofit getRetrofit() { if (retrofit == nul
摘要由CSDN通过智能技术生成

项目一直在用okgo,新项目打算使用retrofit,看了一下,自己记录一下基本使用。。。

1.首先单例一个Retrofit

public class RetrofitUtils
{
    public static Retrofit retrofit;
    public static final String END_POINT = "xxxxx";

    public static Retrofit getRetrofit()
    {
        if (retrofit == null)
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl(END_POINT) //设置网络请求的Url地址
//                    .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

END_POINT 就是你后台接口网址里重复存在的内容,类似www.baidu.com,github.com,这里仅设置一个baseurl

2.每一个api对应一个interface——get/post请求

retrofit的复杂点就在与这个Interface,和里面的一堆注解,我这边只简单弄了些get/post请求

①.post请求

 public interface Login
    {
        @FormUrlEncoded
        @POST("driver/driverLogin")
        Call<ResponseBody> login(@FieldMap Map<String, String> map);
    }

@FormUrlEncoded、@POST和@FieldMap是必须写上的,其中@POST后面括号里的内容是你这个接口网址,加上你的baseurl,你整个的接口url就变成

String url = END_POINT + "driver/driverLogin";

而上面的login,这个是随便写的,比如我这个接口的功能是登录,于是就叫login

具体调用:(interface我全部写在Api.class里)

 Api.Login login = retrofit.create(Api.Login.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", "18512345678");
        Call<ResponseBody> call = login.login(map);
        call.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值