热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于数据挖掘的旅游推荐APP(三):热门景点模块

该模块数据来源于百度搜索风云榜之今日风景名胜排行榜(链接),通过调用托管在神箭手云上的API接口可以实现数据的实时抓取,可以参考这里。效果图:

        该模块数据来源于百度搜索风云榜之今日风景名胜排行榜(链接),通过调用托管在神箭手云上的API接口可以实现数据的实时抓取,可以参考这里。

        效果图:

                      

        接着上一篇继续说,该模块对应LauchFragment,代码如下:

LaunchFragment.java

public class LaunchFragment extends Fragment {

    private static final String TAG="LaunchFragment";
    private RecyclerView mRecyclerView;
    private List mItems= new ArrayList<>(); //这里必须写= new ArrayList<>(),否则可能空指针报错
                        //多线程原因,后台联网下载时,主线程运行,当运行到getItemCount时报错

    public static LaunchFragment newInstance(){
        return new LaunchFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        new FetchItemsTask().execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_launch,container,false);
        mRecyclerView=(RecyclerView) v.findViewById(R.id.hotspot_recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        setupAdapter();
        return v;
    }

    private void setupAdapter() {
        if (isAdded()) {
            //mItems=new BaiDuTopFetchr().fetchItems();//由于多线程运行,需要先判断fragment是否和activity连接
            mRecyclerView.setAdapter(new HotSpotAdapter(mItems));
        }
    }

    private class HotSpotHolder extends RecyclerView.ViewHolder{
        private TextView mRankTextView;
        private TextView mKeywordTextView;
        private TextView mIndexTextView;
        private HotSpotRankItem mHotSpotRankItem;

        public HotSpotHolder(View itemView) {
            super(itemView);
            mRankTextView=(TextView)itemView.findViewById(R.id.hotspot_rank);
            mKeywordTextView=(TextView)itemView.findViewById(R.id.hotspot_keyword);
            mIndexTextView=(TextView)itemView.findViewById(R.id.hotspot_index);
        }

        public void bindHotSpot(HotSpotRankItem hotspot){
            mRankTextView.setText(hotspot.getRank());
            mKeywordTextView.setText(hotspot.getKeyword());
            mIndexTextView.setText("搜索指数:"+hotspot.getIndex());
        }
    }

    private class HotSpotAdapter extends RecyclerView.Adapter{
        private List mHotSpotRankItems;

        public HotSpotAdapter(List hotSpotRankItems) {
            mHotSpotRankItems = hotSpotRankItems;
        }

        @Override
        public HotSpotHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.list_item_hotspot,parent,false);
            return new HotSpotHolder(view);
        }

        @Override
        public void onBindViewHolder(HotSpotHolder holder, int position) {
            HotSpotRankItem hotspot=mHotSpotRankItems.get(position);
            holder.bindHotSpot(hotspot);
        }

        @Override
        public int getItemCount() {
            return mHotSpotRankItems.size();
        }
    }

    private class FetchItemsTask extends AsyncTask> {
        @Override
        protected List doInBackground(Void... params) {
            Log.i(TAG,"do in back");
            return new BaiDuTopFetchr().fetchItems();
        }

        @Override
        protected void onPostExecute(List hotSpotRankItems) {
            Log.i(TAG,"return otems");
            mItems=hotSpotRankItems;
            setupAdapter();
        }
    }
}

        API调用及返回的json数据解析:

BaiDuTopFetchr.java

public class BaiDuTopFetchr {
    private String mAppid="80b574f8abf4224cee648bb060984015";
    private String mHttpUrl = "http://api.shenjian.io/";
    private String mHttpArg = "appid="+mAppid;

    private static final String TAG = "BaiDuTopFetchr";

    public  String request(String httpUrl, String httpArg) {
        BufferedReader reader=null;
        String result=null;
        StringBuffer sbf=new StringBuffer();
        httpUrl=httpUrl+"?"+httpArg;
        try {
            URL url=new URL(httpUrl);
            HttpURLConnection cOnnection=(HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is=connection.getInputStream();
            reader=new BufferedReader(new InputStreamReader(is,"UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List fetchItems() {

        List items = new ArrayList<>();

        try {
            String jsOnString= request(mHttpUrl,mHttpArg);
            Log.i(TAG, "Received JSON: " + jsonString);
            JSONObject jsOnBody= new JSONObject(jsonString);
            parseItems(items, jsonBody);
        } catch (IOException ioe) {
            Log.e(TAG, "Failed to fetch items", ioe);
        } catch (JSONException je) {
            Log.e(TAG, "Failed to parse JSON", je);
        }

        return items;
    }

    private void parseItems(List items, JSONObject jsonBody)
            throws IOException, JSONException {

        JSONArray hotSpotJsOnArray= jsonBody.getJSONArray("data");

        for (int i = 0; i  
 

HotSpotRankItem.java

public class HotSpotRankItem {
    private String mRank;
    private String mKeyword;
    private String mIndex;

    public String getRank() {
        return mRank;
    }

    public void setRank(String rank) {
        mRank = rank;
    }

    public String getKeyword() {
        return mKeyword;
    }

    public void setKeyword(String keyword) {
        mKeyword = keyword;
    }

    public String getIndex() {
        return mIndex;
    }

    public void setIndex(String index) {
        mIndex = index;
    }

}

布局文件:

fragment_launch.xml


list_item_hotspot.xml




        

        

        

源代码在前面博客里。

推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
author-avatar
朱小小喵喵_972
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有