retrofit网络请求

    依赖

compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'

1.创建一个Connstand

public class Connstand {
    public final static String URL_PATH="https://www.apiopen.top/";

}

2.创建一个MyServerInterface接口

public interface MyServerInterface {
    @GET("satinApi?")
    Call<ResultDatas> getDatas(@Query ("type")int type,@Query ("page")int page);
    @GET("satinApi?")
    Call<SHIpinDatas> getDatas02(@Query ("type")int type, @Query ("page")int page);

}

3.在main里面调用

 Retrofit retrofit=new Retrofit.Builder ()
                .addConverterFactory (GsonConverterFactory.create ())
                .baseUrl (Connstand.URL_PATH)
                .build ();
        MyServerInterface serverInterface = retrofit.create (MyServerInterface.class);
        serverInterface.getDatas02 (type,page).enqueue (new Callback<SHIpinDatas> () {
            @Override
            public void onResponse(Call<SHIpinDatas> call, Response<SHIpinDatas> response) {
                List<SHIpinDatas.DataBean> data = response.body ().getData ();
                ShiAdaper shiAdaper=new ShiAdaper (getActivity (),data);
                shipinview.setAdapter (shiAdaper);
                shipinview.setLayoutManager (new LinearLayoutManager (getActivity (),LinearLayoutManager.VERTICAL,false));
            }

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

            }
        });

扫描二维码关注公众号,回复: 3865506 查看本文章

猜你喜欢

转载自blog.csdn.net/wumeng5211314/article/details/80728106