Andriod retrofit 入门

本文介绍如何使用Retrofit结合OkHttp来调用GitHub API获取用户信息。通过具体代码示例展示了Retrofit接口定义、OkHttp客户端配置、请求参数设置及响应处理等关键步骤。
   compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
//java format
public class Model {
}
//must be a model
 public interface GitAPi {
    @GET("/users/{user}")
     Call<Model> getFeed(@Path("user") String user);
}

 OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(15, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GitAPi service = retrofit.create(GitAPi.class);
        Call<Model>  call = service.getFeed("132");
        call.enqueue(new Callback<Model>() {
            @Override
            public void onResponse(Call<Model> call, Response<Model> response) {
            Log.e("tag",response.body().getName());
                tv.setText(response.body().getName());
            }

            @Override
            public void onFailure(Call<Model> call, Throwable t) {
                Log.e("tag",t.toString());
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值