日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于数据挖掘的旅游推荐APP(三):热门景点模块 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??????? 該模塊數(shù)據(jù)來(lái)源于百度搜索風(fēng)云榜之今日風(fēng)景名勝排行榜(鏈接),通過調(diào)用托管在神箭手云上的API接口可以實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)抓取,可以參考這里。

??????? 效果圖:

??????????????????????

??????? 接著上一篇繼續(xù)說,該模塊對(duì)應(yīng)LauchFragment,代碼如下:

LaunchFragment.java

public class LaunchFragment extends Fragment {private static final String TAG="LaunchFragment";private RecyclerView mRecyclerView;private List<HotSpotRankItem> mItems= new ArrayList<>(); //這里必須寫= new ArrayList<>(),否則可能空指針報(bào)錯(cuò)//多線程原因,后臺(tái)聯(lián)網(wǎng)下載時(shí),主線程運(yùn)行,當(dāng)運(yùn)行到getItemCount時(shí)報(bào)錯(cuò)public static LaunchFragment newInstance(){return new LaunchFragment();}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setRetainInstance(true);new FetchItemsTask().execute();}@Overridepublic 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();//由于多線程運(yùn)行,需要先判斷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("搜索指數(shù):"+hotspot.getIndex());}}private class HotSpotAdapter extends RecyclerView.Adapter<HotSpotHolder>{private List<HotSpotRankItem> mHotSpotRankItems;public HotSpotAdapter(List<HotSpotRankItem> hotSpotRankItems) {mHotSpotRankItems = hotSpotRankItems;}@Overridepublic HotSpotHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view=LayoutInflater.from(getActivity()).inflate(R.layout.list_item_hotspot,parent,false);return new HotSpotHolder(view);}@Overridepublic void onBindViewHolder(HotSpotHolder holder, int position) {HotSpotRankItem hotspot=mHotSpotRankItems.get(position);holder.bindHotSpot(hotspot);}@Overridepublic int getItemCount() {return mHotSpotRankItems.size();}}private class FetchItemsTask extends AsyncTask<Void,Void,List<HotSpotRankItem>> {@Overrideprotected List<HotSpotRankItem> doInBackground(Void... params) {Log.i(TAG,"do in back");return new BaiDuTopFetchr().fetchItems();}@Overrideprotected void onPostExecute(List<HotSpotRankItem> hotSpotRankItems) {Log.i(TAG,"return otems");mItems=hotSpotRankItems;setupAdapter();}} }

????????API調(diào)用及返回的json數(shù)據(jù)解析:

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<HotSpotRankItem> fetchItems() {List<HotSpotRankItem> 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<HotSpotRankItem> items, JSONObject jsonBody)throws IOException, JSONException {JSONArray hotSpotJsonArray = jsonBody.getJSONArray("data");for (int i = 0; i < hotSpotJsonArray.length(); i++) {JSONObject hotSpotJsonObject = hotSpotJsonArray.getJSONObject(i);HotSpotRankItem item = new HotSpotRankItem();item.setRank(hotSpotJsonObject.getString("rank"));item.setKeyword(hotSpotJsonObject.getString("keyword"));item.setIndex(hotSpotJsonObject.getString("index"));items.add(item);}} }

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

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/hotspot_recycler_view"android:layout_width="match_parent"android:layout_height="wrap_content"/>

list_item_hotspot.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/hotspot_rank"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:padding="4dp"/><TextViewandroid:id="@+id/hotspot_keyword"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:padding="4dp"/><TextViewandroid:id="@+id/hotspot_index"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:padding="4dp"/></LinearLayout>源代碼在前面博客里。

總結(jié)

以上是生活随笔為你收集整理的基于数据挖掘的旅游推荐APP(三):热门景点模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。