Rerotfit的网络请求方式

//是一个代理对象

public class Api {

    public static String BASE_URL="http://service.meiyinkeqiu.com/";
    public static String NEW_URL="http://gank.io/api/";
    public static String STORE_URL="http://www.93.gov.cn/93app/";

}

//请求的方式

public interface ApiService {
    /**
     * 无参get请求
     * http://service.meiyinkeqiu.com/service/ads/cptj
     *
     * @return
     */
    //通过注解声明请求方式
    @GET("service/ads/cptj")
    Call<UserInfo> getNoParmars();


    /**
     * 有参get请求
     * http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
     *
     * @return
     */
    @GET("data/%E7%A6%8F%E5%88%A9/10/{page}")
    Call<NewInfo> getHasParmars(@Path("page") int page);


    /**
     * http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0
     * http://www.93.gov.cn/93app/data.do
     * channelId
     * startNum
     * 拼接 ? &
     * 为主
     */
    @GET("data.do")
    Call<StoreInfo> getHasParmars2(@Query("channelId") int channelId, @Query("startNum") int startNum);
}

//这是一个请求的方法

 private void getNoParmar() {
        //创建HttpLoggingInterceptor日志拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("xxx", message);
            }
        });
        //创建OkHttpClient
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
        //创建Retrofit
        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Api.BASE_URL).build();
        //通过动态代理获取代理对象
        ApiService apiService = retrofit.create(ApiService.class);
        //得到Call
        Call<UserInfo> noParmars = apiService.getNoParmars();
        noParmars.enqueue(new Callback<UserInfo>() {
            //回调在主线程
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                UserInfo userInfo = response.body();
                List<List<String>> dropdown = userInfo.getDropdown();
                List<String> list = dropdown.get(0);
                String str = list.get(0);
                Log.i("xxx", str);


            }


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


            }
        });


    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值