Retrofit 2之get(自用,评论不回)

1.导入

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

interface FilmInter请求接口

public interface FilmInter {
//如果get请求后面拼接了键值对的操作
   @GET("splash/splashList")
   Call getFilmInfo(@QueryMap ArrayMap params);
//只有一个键值对的时候,id=0,传入0就可以了
    @GET("splash/splashList")
    CallgetFilmInfo2(@Query("id") String id);

    //@Path   的使用
//    @GET("splash/{list}")
//    CallgetFilmInfo(@Path("list") String list);
}

RetrofitActivity01(java代码)

public class RetrofitActivity01 extends AppCompatActivity {

    private TextView mTv;
    private static final String BASE_URL="http://www.moviebase.cn/uread/app/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit01);
        mTv = (TextView) findViewById(R.id.tv1);
    }

    public void onClick(View view) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(BASE_URL);   //设置公共的地址
        builder.addConverterFactory(GsonConverterFactory.create());     //因为是json数据,使用gson解析
//        builder.client(client)     //可以写可不写,不写默认用原生的客户端。
        Retrofit retrofit = builder.build();

        FilmInter filmInter = retrofit.create(FilmInter.class);

        ArrayMapmap = new ArrayMap<>();
        map.put("platform","2");
        map.put("appVersion","1.7.0");
        map.put("osType","2");
        Call call = filmInter.getFilmInfo(map);

        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                FilmInfo  info = response.body();
                mTv.setText(info.getSplash().toString());
            }
            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });

    }
}
用到的类
public class FilmInfo {
    private Splash splash;
    private String retCode;
    private String requestId;

    public Splash getSplash() {
        return splash;
    }

    public void setSplash(Splash splash) {
        this.splash = splash;
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public static class Splash {

        private String resolutionWidth;// private String 640private String ,
        private String jopUrl;// private String www.baidu.comprivate String ,
        private String osType;// private String 1private String ,
        private String contentType;// private String 0private String ,
        private String endTime;// private String 1486180800private String ,
        private String type;// private String 0private String ,
        private String transparency;// private String 0private String ,
        private String id;// private String 1private String ,
        private String displayTime;// private String 3.4private String ,
        private String title;// private String ios分辨率640*960private String ,
        private String ifShading;// private String 1private String ,
        private String resolutionHeight;// private String 960private String ,
        private String imgurl;// private String http://img.moviebase.cn/img/other/2017/02/4d98466d6ac049ec9077a191f7450fcd.jpegprivate String

        public String getResolutionWidth() {
            return resolutionWidth;
        }

        public void setResolutionWidth(String resolutionWidth) {
            this.resolutionWidth = resolutionWidth;
        }

        public String getJopUrl() {
            return jopUrl;
        }

        public void setJopUrl(String jopUrl) {
            this.jopUrl = jopUrl;
        }

        public String getOsType() {
            return osType;
        }

        public void setOsType(String osType) {
            this.osType = osType;
        }

        public String getContentType() {
            return contentType;
        }

        public void setContentType(String contentType) {
            this.contentType = contentType;
        }

        public String getEndTime() {
            return endTime;
        }

        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getTransparency() {
            return transparency;
        }

        public void setTransparency(String transparency) {
            this.transparency = transparency;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getDisplayTime() {
            return displayTime;
        }

        public void setDisplayTime(String displayTime) {
            this.displayTime = displayTime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getIfShading() {
            return ifShading;
        }

        public void setIfShading(String ifShading) {
            this.ifShading = ifShading;
        }

        public String getResolutionHeight() {
            return resolutionHeight;
        }

        public void setResolutionHeight(String resolutionHeight) {
            this.resolutionHeight = resolutionHeight;
        }

        public String getImgurl() {
            return imgurl;
        }

        public void setImgurl(String imgurl) {
            this.imgurl = imgurl;
        }

        @Override
        public String toString() {
            return "Splash{" +
                    "resolutionWidth='" + resolutionWidth + '\'' +
                    ", jopUrl='" + jopUrl + '\'' +
                    ", osType='" + osType + '\'' +
                    ", contentType='" + contentType + '\'' +
                    ", endTime='" + endTime + '\'' +
                    ", type='" + type + '\'' +
                    ", transparency='" + transparency + '\'' +
                    ", id='" + id + '\'' +
                    ", displayTime='" + displayTime + '\'' +
                    ", title='" + title + '\'' +
                    ", ifShading='" + ifShading + '\'' +
                    ", resolutionHeight='" + resolutionHeight + '\'' +
                    ", imgurl='" + imgurl + '\'' +
                    '}';
        }
    }
}

你可能感兴趣的:(android)