Andorid Retrofit2

Retrofit2:

  作用:通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果。

举列:

定义一个接口,用于访问使用。

public interface IServiceApi {

    @FormUrlEncoded

    @POST("login")

    Call<LoginResult> login(@Field("name") String name, @Field("pwd") String pwd);

    @GET("getinfo")

    Call<UserInfo> getinfo(@Query("token") String token);

    @GET("getinfo2")

    Call<UserInfo> getinfo2(@Query("token") String token);

}

  调用接口1.可以在main中调用,因为是通过异步执行(enqueue)

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8089/").build();

        IServiceApi api = retrofit.create(IServiceApi.class);

        Call<LoginResult> call = api.login("test", "test1234");

        call.enqueue(new Callback<LoginResult>() {

            @Override

            public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {

                LoginResult loginResult = response.body();

                if(loginResult != null) {

                }

            }

 

            @Override

            public void onFailure(Call<LoginResult> call, Throwable t) {

                Log.i(tag, "ex " + t.getMessage());

            }

        });

  调用接口2.可以在main中调用,因为是通过异步执行(enqueue)

 

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8089/").build();

        IServiceApi api = retrofit.create(IServiceApi.class);

        Call<UserInfo> call = api.getinfo("testtesttest");

        call.enqueue(new Callback<UserInfo>() {

            @Override

            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {

                UserInfo userInfo = response.body();

                if(userInfo != null) {

                }

            }

 

            @Override

            public void onFailure(Call<UserInfo> call, Throwable t) {

                Log.i(tag, "ex " + t.getMessage());

            }

        });

  同步调用接口3.

 

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8089/").build();

        IServiceApi api = retrofit.create(IServiceApi.class);

        Call<LoginResult> call = api.login("test", "test1234");

        try {

            Response<LoginResult> resultResponse = call.execute();

            LoginResult result = resultResponse.body();

        }catch (Exception e){

            e.printStackTrace();

        }

  A:异步调用

  1、创建一个Retrofit对象。

  2、retrofit.create(IServiceApi.class); // 使用Proxy.newProxyInstance 创建一个接口的代理对象。内部使用 ServiceMethod配合OkHttpCall调用,构造他们需要的对象数据

  3、调用enqueue,内部构造okhttp3.Call对象,执行对象的enqueue方法。然后在okhttp3$Callback方法中,调用retrofit2的 回调,赋值成功和失败。

  B:同步调用

  1、同理,同步调用,最后也是调用的。okhtt3.Call的execute方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值