Retrofit

1.导依赖

compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

2.建一个接口注解GET

public interface MyApp {
        接口
    //http://service.meiyinkeqiu.com/service/ads/cptj
    后半截写在GET里
    @GET("service/ads/cptj")
    InFo是一个bean类
    Call<InFo>getInFo();

}
3.在Acitytiv写的代码
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getInFo();
    }

    public void getInFo() {
    日志拦截器
        HttpLoggingInterceptor h=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d(">>>",message);
            }
        });
   // OkHttp
        OkHttpClient ok=new OkHttpClient.Builder().addInterceptor(h).build();
        Retrofit re=new Retrofit.Builder().client(ok).addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://service.meiyinkeqiu.com/").build();
        MyApp myApp = re.create(MyApp.class);
        final Call<InFo> inFo = myApp.getInFo();
        inFo.enqueue(new Callback<InFo>() {
            @Override
            public void onResponse(Call<InFo> call, Response<InFo> response) {
                InFo body = response.body();
                List<List<String>> dropdown = body.getDropdown();
                for (int i = 0; i <dropdown.size() ; i++) {
                    List<String> strings = dropdown.get(i);
                    for (int j = 0; j <strings.size() ; j++) {
                        String s = strings.get(j);
                        Log.d("<<<<<<",s);

                    }

                }
            }

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

            }
        });

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值