Git(分布式版本控制系统)

这只是一个练习案例

首先,需要导入的依赖有:

    compile 'com.jakewharton:butterknife:8.8.1'
    compile 'com.jakewharton:butterknife-compiler:8.8.1'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'


其次预先设置好网络权限:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


下面就可以开始项目的创建了:

首先是布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.day01_rikao.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

layout_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_weight="1"
        android:id="@+id/iv_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_weight="1"
        android:id="@+id/tv_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

</LinearLayout>



接着是Java文件:

Film.java

package com.example.day01_rikao;

import java.util.List;

/**
 * Created by Me on 2017/12/25.
 */

public class Film {
    /**
     * status : 0
     * msg : OK
     * data : [{"postid":"53160","title":"如果能预知生死,你是否能坦然接受死亡?","wx_small_app_title":"如果能预知生死,你是否能坦然接受死亡?","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-18/5a374bd841156_cut.jpeg","rating":"7.6","duration":"564","publish_time":"1514131560","like_num":"354","share_num":"217","post_type":"1","cates":[{"cateid":"16","catename":"动画"}],"request_url":"http://app.vmoiver.com/53160?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"16","catename":"动画"}]},{"postid":"53191","title":"法国青春洋溢奇幻短片:返老还童","wx_small_app_title":"法国青春洋溢奇幻短片:返老还童","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-25/5a40c2d140c76_cut.jpeg","rating":"7.8","duration":"218","publish_time":"1514131500","like_num":"263","share_num":"154","post_type":"1","cates":[{"cateid":"18","catename":"音乐"}],"request_url":"http://app.vmoiver.com/53191?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"18","catename":"音乐"}]},{"postid":"53141","title":"法国大脑洞奇趣动画:水手与人鱼","wx_small_app_title":"法国大脑洞奇趣动画:水手与人鱼","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-13/5a30cd4014c7a_cut.jpeg","rating":"7.5","duration":"355","publish_time":"1514131440","like_num":"182","share_num":"163","post_type":"1","cates":[{"cateid":"16","catename":"动画"}],"request_url":"http://app.vmoiver.com/53141?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"16","catename":"动画"}]},{"postid":"53204","title":"50部影史经典电影混剪「戏梦人生」","wx_small_app_title":"50部影史经典电影混剪「戏梦人生」","pid":"1","app_fu_title":"","is_xpc":"1","is_promote":"0","is_xpc_zp":"1","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-22/5a3cae8f931ac_cut.jpeg","rating":"7.4","duration":"238","publish_time":"1514131380","like_num":"324","share_num":"203","post_type":"1","cates":[{"cateid":"44","catename":"混剪"}],"request_url":"http://app.vmoiver.com/53204?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"44","catename":"混剪"}]},{"postid":"52789","title":"白日梦想家:旅行是人生的万能治愈剂","wx_small_app_title":"白日梦想家:旅行是人生的万能治愈剂","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmoiver.com/Uploads/cover/2017-10-16/59e4aaa63a577_cut.jpeg","rating":"7.8","duration":"241","publish_time":"1514131320","like_num":"278","share_num":"143","post_type":"1","cates":[{"cateid":"18","catename":"音乐"}],"request_url":"http://app.vmoiver.com/52789?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"18","catename":"音乐"}]},{"postid":"53184","title":"最特别的愿望清单","wx_small_app_title":"最特别的愿望清单","pid":"1","app_fu_title":"迟到的圣诞老人,不迟到的圣诞心愿","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"1","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-22/5a3d0564d38fe_cut.jpeg","rating":"7.8","duration":"137","publish_time":"1514045460","like_num":"921","share_num":"1487","post_type":"3","cates":[{"cateid":"78","catename":"生活"}],"request_url":"http://app.vmoiver.com/53184?qingapp=app_new","ispromote":"0","isalbum":"1","cate":[{"cateid":"78","catename":"生活"}]},{"postid":"53207","title":"超浓圣诞味钢琴演奏,1台钢琴点亮50万支灯泡","wx_small_app_title":"超浓圣诞味钢琴演奏,1台钢琴点亮50万支灯泡","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-22/5a3cd735cf99e_cut.jpeg","rating":"7.2","duration":"259","publish_time":"1514045160","like_num":"769","share_num":"1194","post_type":"1","cates":[{"cateid":"18","catename":"音乐"}],"request_url":"http://app.vmoiver.com/53207?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"18","catename":"音乐"}]},{"postid":"53201","title":"萌趣暖心节日短片「最好的祝福」","wx_small_app_title":"萌趣暖心节日短片「最好的祝福」","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-21/5a3b91d6127e9_cut.jpeg","rating":"7.1","duration":"122","publish_time":"1514045100","like_num":"336","share_num":"432","post_type":"1","cates":[{"cateid":"16","catename":"动画"}],"request_url":"http://app.vmoiver.com/53201?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"16","catename":"动画"}]},{"postid":"53183","title":"如果驾校教练是这样的\u2026\u2026","wx_small_app_title":"如果驾校教练是这样的\u2026\u2026","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-20/5a3a12cc5ff5e_cut.jpeg","rating":"7.5","duration":"310","publish_time":"1514045040","like_num":"327","share_num":"298","post_type":"1","cates":[{"cateid":"13","catename":"广告"}],"request_url":"http://app.vmoiver.com/53183?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"13","catename":"广告"}]},{"postid":"53202","title":"末日来袭,我们是否要坐以待毙?","wx_small_app_title":"末日来袭,我们是否要坐以待毙?","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"http://cs.vmovier.com/Uploads/cover/2017-12-23/5a3e6217b6429_cut.jpeg","rating":"7.6","duration":"241","publish_time":"1514044980","like_num":"306","share_num":"155","post_type":"1","cates":[{"cateid":"8","catename":"搞笑"}],"request_url":"http://app.vmoiver.com/53202?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"8","catename":"搞笑"}]}]
     */

    private String status;
    private String msg;
    private List<DataBean> data;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * postid : 53160
         * title : 如果能预知生死,你是否能坦然接受死亡?
         * wx_small_app_title : 如果能预知生死,你是否能坦然接受死亡?
         * pid : 1
         * app_fu_title :
         * is_xpc : 0
         * is_promote : 0
         * is_xpc_zp : 0
         * is_xpc_cp : 0
         * is_xpc_fx : 0
         * is_album : 0
         * tags :
         * recent_hot : 0
         * discussion : 0
         * image : http://cs.vmovier.com/Uploads/cover/2017-12-18/5a374bd841156_cut.jpeg
         * rating : 7.6
         * duration : 564
         * publish_time : 1514131560
         * like_num : 354
         * share_num : 217
         * post_type : 1
         * cates : [{"cateid":"16","catename":"动画"}]
         * request_url : http://app.vmoiver.com/53160?qingapp=app_new
         * ispromote : 0
         * isalbum : 0
         * cate : [{"cateid":"16","catename":"动画"}]
         */

        private String postid;
        private String title;
        private String wx_small_app_title;
        private String pid;
        private String app_fu_title;
        private String is_xpc;
        private String is_promote;
        private String is_xpc_zp;
        private String is_xpc_cp;
        private String is_xpc_fx;
        private String is_album;
        private String tags;
        private String recent_hot;
        private String discussion;
        private String image;
        private String rating;
        private String duration;
        private String publish_time;
        private String like_num;
        private String share_num;
        private String post_type;
        private String request_url;
        private String ispromote;
        private String isalbum;
        private List<CatesBean> cates;
        private List<CateBean> cate;

        public String getPostid() {
            return postid;
        }

        public void setPostid(String postid) {
            this.postid = postid;
        }

        public String getTitle() {
            return title;
        }

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

        public String getWx_small_app_title() {
            return wx_small_app_title;
        }

        public void setWx_small_app_title(String wx_small_app_title) {
            this.wx_small_app_title = wx_small_app_title;
        }

        public String getPid() {
            return pid;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

        public String getApp_fu_title() {
            return app_fu_title;
        }

        public void setApp_fu_title(String app_fu_title) {
            this.app_fu_title = app_fu_title;
        }

        public String getIs_xpc() {
            return is_xpc;
        }

        public void setIs_xpc(String is_xpc) {
            this.is_xpc = is_xpc;
        }

        public String getIs_promote() {
            return is_promote;
        }

        public void setIs_promote(String is_promote) {
            this.is_promote = is_promote;
        }

        public String getIs_xpc_zp() {
            return is_xpc_zp;
        }

        public void setIs_xpc_zp(String is_xpc_zp) {
            this.is_xpc_zp = is_xpc_zp;
        }

        public String getIs_xpc_cp() {
            return is_xpc_cp;
        }

        public void setIs_xpc_cp(String is_xpc_cp) {
            this.is_xpc_cp = is_xpc_cp;
        }

        public String getIs_xpc_fx() {
            return is_xpc_fx;
        }

        public void setIs_xpc_fx(String is_xpc_fx) {
            this.is_xpc_fx = is_xpc_fx;
        }

        public String getIs_album() {
            return is_album;
        }

        public void setIs_album(String is_album) {
            this.is_album = is_album;
        }

        public String getTags() {
            return tags;
        }

        public void setTags(String tags) {
            this.tags = tags;
        }

        public String getRecent_hot() {
            return recent_hot;
        }

        public void setRecent_hot(String recent_hot) {
            this.recent_hot = recent_hot;
        }

        public String getDiscussion() {
            return discussion;
        }

        public void setDiscussion(String discussion) {
            this.discussion = discussion;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getRating() {
            return rating;
        }

        public void setRating(String rating) {
            this.rating = rating;
        }

        public String getDuration() {
            return duration;
        }

        public void setDuration(String duration) {
            this.duration = duration;
        }

        public String getPublish_time() {
            return publish_time;
        }

        public void setPublish_time(String publish_time) {
            this.publish_time = publish_time;
        }

        public String getLike_num() {
            return like_num;
        }

        public void setLike_num(String like_num) {
            this.like_num = like_num;
        }

        public String getShare_num() {
            return share_num;
        }

        public void setShare_num(String share_num) {
            this.share_num = share_num;
        }

        public String getPost_type() {
            return post_type;
        }

        public void setPost_type(String post_type) {
            this.post_type = post_type;
        }

        public String getRequest_url() {
            return request_url;
        }

        public void setRequest_url(String request_url) {
            this.request_url = request_url;
        }

        public String getIspromote() {
            return ispromote;
        }

        public void setIspromote(String ispromote) {
            this.ispromote = ispromote;
        }

        public String getIsalbum() {
            return isalbum;
        }

        public void setIsalbum(String isalbum) {
            this.isalbum = isalbum;
        }

        public List<CatesBean> getCates() {
            return cates;
        }

        public void setCates(List<CatesBean> cates) {
            this.cates = cates;
        }

        public List<CateBean> getCate() {
            return cate;
        }

        public void setCate(List<CateBean> cate) {
            this.cate = cate;
        }

        public static class CatesBean {
            /**
             * cateid : 16
             * catename : 动画
             */

            private String cateid;
            private String catename;

            public String getCateid() {
                return cateid;
            }

            public void setCateid(String cateid) {
                this.cateid = cateid;
            }

            public String getCatename() {
                return catename;
            }

            public void setCatename(String catename) {
                this.catename = catename;
            }
        }

        public static class CateBean {
            /**
             * cateid : 16
             * catename : 动画
             */

            private String cateid;
            private String catename;

            public String getCateid() {
                return cateid;
            }

            public void setCateid(String cateid) {
                this.cateid = cateid;
            }

            public String getCatename() {
                return catename;
            }

            public void setCatename(String catename) {
                this.catename = catename;
            }
        }
    }
}


FilmInterFace.java

package com.example.day01_rikao;

import retrofit2.Call;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;

/**
 * Created by Me on 2017/12/25.
 */

public interface FilmInterFace {

    /*
        http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1
        公共地址:http://app.vmoiver.com/
        values(值): apiv3/post/getPostInCate
        参数: cateid=0&p=1

     */

    @POST("apiv3/post/getPostInCate")
    Call<Film> getFilm(@QueryMap android.support.v4.util.ArrayMap map);

}


MainActivity.java

package com.example.day01_rikao;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    /*
            URI:http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1
     */

    private Unbinder mUnbinder;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

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

        mUnbinder = ButterKnife.bind(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        getData();
    }

    private void getData() {

        Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://app.vmoiver.com/");

        builder.addConverterFactory(GsonConverterFactory.create());

        Retrofit build = builder.build();

        FilmInterFace filmInterFace = build.create(FilmInterFace.class);

        ArrayMap map = new ArrayMap();
        map.put("cateid","1");
        map.put("p","0");

        Call film = filmInterFace.getFilm(map);

        film.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Film film = (Film) response.body();

                List<Film.DataBean> data = film.getData();

                mRecyclerView.setAdapter(new MyRecyclerViewAdapter(MainActivity.this, data));
            }

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

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

}


MyRecyclerViewAdapter

package com.example.day01_rikao;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Me on 2017/12/25.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private List<Film.DataBean> list;

    public MyRecyclerViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_recycler, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Picasso.with(context).load(list.get(position).getImage()).into(holder.iv);
        holder.tv.setText(list.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iv;
        TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_id);
            tv = (TextView) itemView.findViewById(R.id.tv_id);
        }
    }
}


到这里,基本就结束了,如果无法正确展示效果的话,那么可能是你的Android Studio版本过低的原因

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭