免费试用

中文化、本土化、云端化的在线跨平台软件开发工具,支持APP、电脑端、小程序、IOS免签等等

android 新闻app开发

在这个信息时代,人们获取新闻的方式已经从传统的报纸、电视逐渐转向移动端。而根据用户的需求和便利性,移动端的新闻应用普及也变成一种趋势。本文将介绍如何开发一款Android的新闻应用。

首先,新闻应用的本质就是一个前端展示,并且获取了数据的应用。而新闻类应用的主要数据来源有两种:一种是从服务器端获取数据,这种方式相对来说操作较为繁琐。需要自己开发服务器和接口,并联系新闻数据的供应商。这里我们将主要介绍第二种方式即通过抓取网络上的数据源获取新闻数据。

一,数据来源

在这里我们会用到一款网络数据抓取的库——Jsoup,该库可以获取网页上的HTML代码,并且提供了操作的方便性,同时Jsoup的操作方式和我们常用的DOM解析很相似,所以很容易上手。安卓studio的使用不做过多展开,不懂的可以去官网查看。

我们可以先定义一个DataModel类,用于描述数据来源的实体类。在这里,我们定义了一个id,title和newsUrl三个属性:

```

public class DataModel {

private int id;

private String title;

private String newsUrl;

//构造方法,省略getter和setter方法

public DataModel(int id, String title, String newsUrl) {

this.id = id;

this.title = title;

this.newsUrl = newsUrl;

}

}

```

接下来,我们需要对于数据源进行操作,尝试去获取网页上的HTML代码:我们以获取天气新闻为例子:

```

public class NetworkDataFetcher {

private static final String NEWS_URL_TOUTIAO = "http://www.toutiao.com/ch/news_hot/";

public static final String USER_AGENT_MOZILLA_5_0_WINDOWS_NT_61_WOW64_APPLE_WEB_KIT537_36_KHTML_GECKO_FIREFOX50_0_1_FIREFOX_ESR_45_6 = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0.1 Firefox/ESR 45.6";

public static final String COOKIE_DISTINCT_ID = "distinct_id=15b7a1f0fa789c-0a244231a9c7f1-b363e65-100200-15b7a1f0fa930c; expires=Thu, 27 Dec 2018 02:43:01 GMT; Max-Age=15552000; path=/; domain=.toutiao.com";

public static final String COOKIE_UCID = "ucid=uskoud0iaaepvaqvvc5bewjlzt7ye2ro42rguhf7vrex9xg69gxpu0_1eo4t4w4&yi=undefined&subch_es=android&subch_id=push_dispatch%3A758762292916063700; expires=Fri, 03 Jan 2020 02:43:01 GMT; Max-Age=31536000; path=/; domain=.toutiao.com";

public static List getNetworkData() throws Exception {

Connection conn = Jsoup.connect(NEWS_URL_TOUTIAO);

conn.header("User-Agent", USER_AGENT_MOZILLA_5_0_WINDOWS_NT_61_WOW64_APPLE_WEB_KIT537_36_KHTML_GECKO_FIREFOX50_0_1_FIREFOX_ESR_45_6);

conn.header("Cookie", COOKIE_UCID);

conn.header("Cookie", COOKIE_DISTINCT_ID);

Document document = conn.get();

Element element = document.select("ul.list.list-paddingleft-0").first();

List dataModelList = new ArrayList<>();

Elements elements = element.getElementsByTag("li");

for (Element e : elements) {

Element aTag = e.getElementsByTag("a").first();

Element titleTag = aTag.getElementsByTag("title").first();

String title = titleTag.html();

String urlString = aTag.attr("href");

DataModel dataModel = new DataModel(0, title, urlString);

dataModelList.add(dataModel);

System.out.println(title + ":" + urlString);

}

return dataModelList;

}

}

```

我们这里主要是获取了网页中新闻标题和新闻的URL,将其缓存到dataModelList中。

二,列表页面的实现

获取了数据后,我们需要在页面上展示数据,具体而言就是在一个列表页面上展示获取到的数据。我们在xml布局文件中使用了RecyclerView作为列表控件,同时还定义了一个卡片布局来展示每一条新闻的标题。代码如下:

```

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/id_recycler_view"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:id="@+id/id_news_card_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="@dimen/card_margin"

android:padding="@dimen/card_padding">

android:id="@+id/id_news_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="@dimen/news_title_size"

android:textColor="@color/color_black"

android:paddingBottom="@dimen/news_padding_bottom"

android:text="DemoNewsTitle" />

```

在Activity的实现中,我们我们将数据列表和卡片布局绑定在RecyclerView中,最终通过Adapter的方式将数据和列表的视图绑定,展现在UI界面中。代码如下:

```

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;

private LinearLayoutManager mLayoutManager;

private List mDataList;

private DataAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mRecyclerView = (RecyclerView) findViewById(R.id.id_recycler_view);

mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);

mRecyclerView.setLayoutManager(mLayoutManager);

mDataList = new ArrayList<>();

try {

mDataList = NetworkDataFetcher.getNetworkData();

} catch (Exception e) {

e.printStackTrace();

}

mAdapter = new DataAdapter(this, mDataList);

mRecyclerView.setAdapter(mAdapter);

}

}

```

三,新闻页面的实现

在列表页面展现了新闻的标题后,需要进入每一个详情页面获取新闻的具体内容。我们可以将展示内容的页面定义为一个Fragment,当点击详情时通过替换和Fragment的方式进入新闻详情页面。局部静态变量DataModel表示将要展示的新闻,NewsFragment类实现了UI界面的初始化以及数据的展现,我们同样使用Jsoup库加载HTML代码,并将拉取到的新闻内容展现在UI界面中。

```

public class NewsFragment extends Fragment {

private DataModel mData;

public NewsFragment() {

// Required empty public constructor

}

public static NewsFragment newInstance(DataModel data) {

NewsFragment fragment = new NewsFragment();

fragment.mData = data;

return fragment;

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.fragment_news, container, false);

TextView textView =(TextView)view.findViewById(R.id.news_content);

textView.setMovementMethod(ScrollingMovementMethod.getInstance());

try {

Document doc = null;

doc = Jsoup.connect(mData.getNewsUrl()).get();

textView.setText(doc.html());

} catch (IOException e) {

e.printStackTrace();

}

return view;

}

}

```

最后的效果如图:列表和新闻的详情页面

![newsapp](https://user-images.githubusercontent.com/38556951/127727888-a0d43b8e-01bd-47ca-9c21-ad090e61b7d3.gif)

以上便是一款新闻类应用的主要实现原理,与大家分享。希望对大家开发类似应用有所启示。


相关知识:
陕西视频app开发团队
陕西视频app开发团队是一支专业的互联网技术团队,致力于为客户提供高质量、高效率的移动应用开发服务。团队成员均来自于陕西省内知名高校的计算机相关专业,有着扎实的技术基础和丰富的开发经验。团队主要负责的是视频类移动应用的开发,包括但不限于短视频、直播、影视等
2024-01-10
如何查询app的开发方
在使用各种手机应用程序时,我们可能会想知道这个应用的开发方是谁,以及这个应用的背景信息。有时候,我们想知道这个应用程序是否安全,是否值得信任。了解应用程序的开发方可以帮助我们更好地了解应用程序的背景信息,从而做出更好的决策。那么,如何查询应用程序的开发方呢
2024-01-10
app开发课程心得体会
在参加app开发课程学习后,我深刻体会到了一个app从无到有的开发过程,也认识到了app开发需要哪些基础知识和技能,同时也领悟到了app开发的一些心得和体会。首先,在app开发之前,我们需要学会使用开发工具。相信大家都知道市面上流行着很多开发工具,如And
2023-06-29
app开发策划ppt
APP开发策划PPT是APP开发过程中非常重要的一步,其工作主要是为了确保APP开发过程中能够得到顺利的进行,并能够达到预期的设计需求和用户需求。下面详细介绍APP开发策划PPT的原理和方法。一、APP开发策划PPT的原理APP开发策划PPT主要是为了在A
2023-06-29
apple系列的免费设计开发助理培训
Apple公司在设计和开发领域一直被认为是最顶尖的,在许多领域都有丰富的经验和技能。如果您想要进入这个领域,苹果公司为您提供了许多免费的设计开发助理培训课程,旨在帮助您开始建立自己的技能。苹果公司提供的免费设计开发助理培训课程主要包括:1. Swift编程
2023-05-06
apple 开发者帐号注册
Apple开发者账号是苹果公司提供的一个开发者注册平台,主要面向在iOS、macOS和watchOS等系统上开发软件和应用程序的开发者。在这个平台上注册账号后,开发者可以获得许多开发工具和资源,用于快速构建出高质量的应用程序。下面,我们将为大家介绍如何注册
2023-05-06