作者:朱小小喵喵_972 | 来源:互联网 | 2023-09-25 18:55
该模块数据来源于百度搜索风云榜之今日风景名胜排行榜(链接),通过调用托管在神箭手云上的API接口可以实现数据的实时抓取,可以参考这里。
效果图:
![](https://img1.php1.cn/3cd4a/24f51/525/e4af1fec4b46df75.jpeg)
接着上一篇继续说,该模块对应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
源代码在前面博客里。