影视分享App(三)(第六周)

 

 

第六周

所花时间

25h左右

代码量

3000行左右

博客量

4篇

学到的知识点

使用python完成词频统计,影视分享APP的开发(还在开发中)

 

 

摘要:第三阶段主要完成了对豆瓣影视数据(json)的提取以及解析,并且通过点击悬浮窗按钮让其显示指定数据的简单测试,解析数据起初使用的方法是Json和JsonArray的读取方法,这两种方法读取格式简单的数据还可以,但是豆瓣影视数据的格式有点复杂,读取起来麻烦,最后通过调用gson,问题相对来说简单了很多。

一、开发阶段三

首先分享一个牛X的json格式解析工具,JSON数据解析工具 使用这个工具可以将我们的json格式的数据生成JavaBean,不管你的json格式数据结构多复杂,都可以实现。生成后直接导入工程即可,这样就不用我们一个一个去设计Javabean了,完成本项目共使用7个Javabean类,而且结构复杂,这就是为什么使用此工具的原因。以下是本项目使用的Javabean类。

/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;
import java.util.List;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class JsonRootBean {

    private int count;
    private int start;
    private int total;
    private List<Subjects> subjects;
    private String title;
    public void setCount(int count) {
         this.count = count;
     }
     public int getCount() {
         return count;
     }

    public void setStart(int start) {
         this.start = start;
     }
     public int getStart() {
         return start;
     }

    public void setTotal(int total) {
         this.total = total;
     }
     public int getTotal() {
         return total;
     }

    public void setSubjects(List<Subjects> subjects) {
         this.subjects = subjects;
     }
     public List<Subjects> getSubjects() {
         return subjects;
     }

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

}
JsonRootBean
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;
import java.util.List;
import java.util.Date;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Subjects {

    private Rating rating;
    private List<String> genres;
    private String title;
    private List<Casts> casts;
    private List<String> durations;
    private int collect_count;
    private Date mainland_pubdate;
    private boolean has_video;
    private String original_title;
    private String subtype;
    private List<Directors> directors;
    private List<Date> pubdates;
    private String year;
    private Images images;
    private String alt;
    private String id;
    public void setRating(Rating rating) {
         this.rating = rating;
     }
     public Rating getRating() {
         return rating;
     }

    public void setGenres(List<String> genres) {
         this.genres = genres;
     }
     public List<String> getGenres() {
         return genres;
     }

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

    public void setCasts(List<Casts> casts) {
         this.casts = casts;
     }
     public List<Casts> getCasts() {
         return casts;
     }

    public void setDurations(List<String> durations) {
         this.durations = durations;
     }
     public List<String> getDurations() {
         return durations;
     }

    public void setCollect_count(int collect_count) {
         this.collect_count = collect_count;
     }
     public int getCollect_count() {
         return collect_count;
     }

    public void setMainland_pubdate(Date mainland_pubdate) {
         this.mainland_pubdate = mainland_pubdate;
     }
     public Date getMainland_pubdate() {
         return mainland_pubdate;
     }

    public void setHas_video(boolean has_video) {
         this.has_video = has_video;
     }
     public boolean getHas_video() {
         return has_video;
     }

    public void setOriginal_title(String original_title) {
         this.original_title = original_title;
     }
     public String getOriginal_title() {
         return original_title;
     }

    public void setSubtype(String subtype) {
         this.subtype = subtype;
     }
     public String getSubtype() {
         return subtype;
     }

    public void setDirectors(List<Directors> directors) {
         this.directors = directors;
     }
     public List<Directors> getDirectors() {
         return directors;
     }

    public void setPubdates(List<Date> pubdates) {
         this.pubdates = pubdates;
     }
     public List<Date> getPubdates() {
         return pubdates;
     }

    public void setYear(String year) {
         this.year = year;
     }
     public String getYear() {
         return year;
     }

    public void setImages(Images images) {
         this.images = images;
     }
     public Images getImages() {
         return images;
     }

    public void setAlt(String alt) {
         this.alt = alt;
     }
     public String getAlt() {
         return alt;
     }

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

}
Subjects
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Rating {

    private int max;
    private double average;
    private Details details;
    private String stars;
    private int min;
    public void setMax(int max) {
         this.max = max;
     }
     public int getMax() {
         return max;
     }

    public void setAverage(double average) {
         this.average = average;
     }
     public double getAverage() {
         return average;
     }

    public void setDetails(Details details) {
         this.details = details;
     }
     public Details getDetails() {
         return details;
     }

    public void setStars(String stars) {
         this.stars = stars;
     }
     public String getStars() {
         return stars;
     }

    public void setMin(int min) {
         this.min = min;
     }
     public int getMin() {
         return min;
     }

}
Rating
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Casts {

    private Avatars avatars;
    private String name_en;
    private String name;
    private String alt;
    private String id;
    public void setAvatars(Avatars avatars) {
         this.avatars = avatars;
     }
     public Avatars getAvatars() {
         return avatars;
     }

    public void setName_en(String name_en) {
         this.name_en = name_en;
     }
     public String getName_en() {
         return name_en;
     }

    public void setName(String name) {
         this.name = name;
     }
     public String getName() {
         return name;
     }

    public void setAlt(String alt) {
         this.alt = alt;
     }
     public String getAlt() {
         return alt;
     }

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

}
Casts
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Avatars {

    private String small;
    private String large;
    private String medium;
    public void setSmall(String small) {
         this.small = small;
     }
     public String getSmall() {
         return small;
     }

    public void setLarge(String large) {
         this.large = large;
     }
     public String getLarge() {
         return large;
     }

    public void setMedium(String medium) {
         this.medium = medium;
     }
     public String getMedium() {
         return medium;
     }

}
Avatars
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Directors {

    private Avatars avatars;
    private String name_en;
    private String name;
    private String alt;
    private String id;
    public void setAvatars(Avatars avatars) {
         this.avatars = avatars;
     }
     public Avatars getAvatars() {
         return avatars;
     }

    public void setName_en(String name_en) {
         this.name_en = name_en;
     }
     public String getName_en() {
         return name_en;
     }

    public void setName(String name) {
         this.name = name;
     }
     public String getName() {
         return name;
     }

    public void setAlt(String alt) {
         this.alt = alt;
     }
     public String getAlt() {
         return alt;
     }

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

}
Directors
/**
  * Copyright 2020 bejson.com 
  */
package com.example.yingshifenxiang.bean;

/**
 * Auto-generated: 2020-03-28 15:55:4
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Images {

    private String small;
    private String large;
    private String medium;
    public void setSmall(String small) {
         this.small = small;
     }
     public String getSmall() {
         return small;
     }

    public void setLarge(String large) {
         this.large = large;
     }
     public String getLarge() {
         return large;
     }

    public void setMedium(String medium) {
         this.medium = medium;
     }
     public String getMedium() {
         return medium;
     }

}
Images

 

有了这些,就可以在MainActivity.java具体操作啦,下面是主要的代码,通过主界面悬浮按钮的点击按钮事件来实现数据的读取,在之前的MainActivity.java基础上修改就可以了。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                ///http://api.douban.com/v2/movie/in_theaters
                //没写android代码,测试http api接口
                //推荐 PostMan post
                //不能用浏览器 get
                //传入的参数 {"start":2,"count":2}
                String url ="http://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a&start=0&count=10";
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(
                        Request.Method.POST,
                        url,
                        //"{\"start\":2,\"count\":2}",
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Log.d("volley",response.toString());
                                /*
                                String  title="";
                                JSONArray subjects=null;
                                int total = 0;
                                try {
                                    total = response.getInt("total");
                                    int start = response.getInt("start");
                                    int count = response.getInt("count");
                                    subjects = response.getJSONArray("subjects");
                                    title = response.get("title").toString();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                //{key2:value2,key1:value1} 可嵌套 value可以是Json  JsonArray
                                Gson gson =new Gson();
                                //Subject s1=gson.fromJson(subjects.getJSONObject(0).toString(), Subject.class);
                                //Subject s2=gson.fromJson(subjects.getJSONObject(1).toString(), Subject.class);
                                Type type=new TypeToken<List<Subject>>(){}.getType();
                                List<Subject> subjectList=gson.fromJson(subjects.toString(),type);
                                //Log.d("ActionButton",s2.title+s2.year);
                                 */

                                Gson gson = new Gson();
                                JsonRootBean b = gson.fromJson(response.toString(), JsonRootBean.class);
                                Subjects s = b.getSubjects().get(0);
                                Snackbar sb = Snackbar.make(view, s.getTitle() + s.getYear(),Snackbar.LENGTH_SHORT);
                                View v = sb.getView();
                                TextView snackbar_text = (TextView)v.findViewById(R.id.snackbar_text);
                                snackbar_text.setTextColor(Color.parseColor("#FFFFFF"));
                                sb.show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.d("volley",error.toString());

                            }
                        }

                );
                MyApplication.addRequest(jsonObjectRequest,"MainActivity");
            }
        });
MainActivity

效果图

 

 到这,第三阶段的任务就基本实现了。后面的任务会更重。。。

posted @ 2020-03-28 21:53  MoooJL  阅读(527)  评论(0编辑  收藏  举报