The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Embedding a Web Server

Usually, Android devices are mobile. Usually, servers are not mobile.

However, occasionally, you may have a valid reason to want to have your Android app expose some sort of open TCP/IP port to other apps, the user, or (eek!) the Internet at large. The “eek!” is because allowing foreign devices access to stuff inside a user’s device is fraught with security issues, as usually Android devices lack configurable firewalls and the other protection measures associated with production-grade servers.

In this chapter, we will explore some reasons for having such a TCP daemon as part of your app, focusing on the most common scenario: serving Web content from your app. We will then examine more closely one embeddable Web server implementation and how you can use it — carefully – in your Android apps.

Prerequisites

In addition to having read the core chapters of this book, you should have some familiarity with setting up a Web server and a Web application. This chapter is not a primer on these topics, but instead focuses on how to do them in the context of an Android app.

Why a Web Server?

The preview of this section did not survive Thanos's finger snap.

Introducing AsyncHttpServer

The preview of this section was whisked away by a shark-infested tornado.

Embedding a Simple Server

The preview of this section left for Hollywood to appear in a reality TV show.

Template-Driven Responses, with Handlebars

The preview of this section was fed to a gremlin, after midnight.

Supporting WebSockets

The preview of this section was stepped on by Godzilla.

Securing the Web Server

The preview of this section apparently resembled a Pokémon.

Towards a Reusable Web Server Service

The preview of this section was fed to a gremlin, after midnight.