Unit 3: Network communication and publishing
Mobile Application Development notes · PTU syllabus (BSIT601)
On this page
Unit summary
Most apps talk to servers and must eventually be published. This unit covers web services, HTTP clients, XML and JSON, the service life cycle and foreground services, preparing for publishing, signing and preparing graphics, and publishing to Google Play (formerly the Android Market).
After this unit you can
- Consume web services with an HTTP client
- Parse XML and JSON
- Create started, bound and foreground services
- Sign and publish an app
PTU syllabus topics
- Web services
- HTTP client
- XML and JSON
- service lifecycle and foreground services
- preparing for publishing
- signing and preparing graphics
- publishing to the Android Market
- 1
Test on devices and emulators
- 2
Set version and remove debug code
- 3
Sign the app
Generate a release key
- 4
Prepare graphics
Icon, screenshots
- 5
Upload to Google Play Console
- 6
Release and monitor
Topic 1
Web services
Message format
XML envelope
JSON or XML
Protocol
HTTP, SMTP and others
HTTP methods: GET, POST, PUT, DELETE
Weight
Heavy, strict contracts (WSDL)
Lightweight, simple
Mobile use
Rare today
Standard for mobile apps
- Typical flow: the app sends an HTTP request to a REST API, the server returns JSON, and the app parses and displays it.
Topic 2
HTTP client
java// Network calls must run off the main (UI) thread; INTERNET permission required
ExecutorService ex = Executors.newSingleThreadExecutor();
ex.execute(() -> {
try {
URL url = new URL("https://api.example.com/courses");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder(); String line;
while ((line = br.readLine()) != null) sb.append(line);
String json = sb.toString();
runOnUiThread(() -> showCourses(json)); // update UI on main thread
} catch (IOException e) { e.printStackTrace(); }
});- Libraries: OkHttp and Retrofit (with Gson or Moshi) simplify requests, Volley handles request queues; the old Apache HttpClient was removed from Android.
Exam tip
Running network code on the main thread throws NetworkOnMainThreadException — always use a background thread, executor or coroutine.
Topic 3
XML and JSON
Structure
Tags and attributes
Key–value pairs, arrays
Size
Verbose
Compact
Parsing in Android
XmlPullParser, DOM, SAX
JSONObject, JSONArray, Gson
Use
RSS feeds, legacy services
Most REST APIs
java// JSON: {"courses":[{"name":"BCA","seats":60},{"name":"B.Sc IT","seats":60}]}
JSONArray arr = new JSONObject(json).getJSONArray("courses");
for (int i = 0; i < arr.length(); i++) {
JSONObject c = arr.getJSONObject(i);
Log.d("API", c.getString("name") + " " + c.getInt("seats"));
}java// XML pull parsing: <course><name>BCA</name></course>
XmlPullParser p = Xml.newPullParser();
p.setInput(inputStream, null);
int ev = p.getEventType();
while (ev != XmlPullParser.END_DOCUMENT) {
if (ev == XmlPullParser.START_TAG && p.getName().equals("name")) Log.d("XML", p.nextText());
ev = p.next();
}Topic 4
Services and the service life cycle
Started by
startService() or startForegroundService()
bindService()
Runs
Until it stops itself (stopSelf) or is stopped
While at least one client is bound
Communication
One-way
Client–server interface (IBinder)
Callbacks
onCreate, onStartCommand, onDestroy
onCreate, onBind, onUnbind, onDestroy
- 1
startService() called
- 2
onCreate() (first time only)
- 3
onStartCommand() — do the work on a background thread
- 4
Service running
- 5
stopSelf() or stopService()
- 6
onDestroy()
Topic 5
Foreground services
- A foreground service performs work the user notices (music playback, navigation, fitness tracking) and must show an ongoing notification; it is less likely to be killed by the system.
javaNotification n = new NotificationCompat.Builder(this, "player")
.setContentTitle("Playing lecture audio").setSmallIcon(R.drawable.ic_play).build();
startForeground(1, n); // inside the service, soon after it starts- Recent Android versions require declaring a foreground service type and permission; for deferrable background work, WorkManager is recommended.
Topic 6
Preparing for publishing
- 1
Remove logging and debug flags
- 2
Set versionCode and versionName in Gradle
- 3
Choose minSdk and targetSdk (Play requires a recent target API)
- 4
Shrink and obfuscate with R8
- 5
Test on several devices and screen sizes
- 6
Write a privacy policy and data safety details
- 7
Build a signed App Bundle (.aab)
Topic 7
Signing the app and preparing graphics
- Signing: every APK or bundle must be digitally signed. Create an upload key in a keystore (Build → Generate Signed Bundle/APK); with Play App Signing, Google holds the app signing key and you sign uploads with the upload key. Losing the key without Play App Signing means you cannot update the app.
- App icon
- 512 × 512 px PNG
- Feature graphic
- 1024 × 500 px
- Screenshots
- At least two phone screenshots; tablet ones optional
- Promo video
- Optional YouTube link
- Adaptive launcher icon
- Foreground and background layers in the app
Topic 8
Publishing to Google Play
- 1
Create a Google Play developer account (one-time fee)
- 2
Create the app and fill in store listing — title, description, graphics
- 3
Complete content rating, target audience, data safety and privacy policy
- 4
Upload the signed App Bundle to a testing track (internal, closed, open)
- 5
Fix issues reported by pre-launch reports
- 6
Roll out to production, optionally staged
- Android Market was renamed Google Play in 2012; apps can also be distributed through other stores or direct APK downloads.
Key terms
- REST
- Lightweight web service style using HTTP methods
- JSON
- Compact key–value data format
- Service
- Component doing work without a user interface
- Foreground service
- Service with an ongoing notification
- App signing
- Digitally signing an app to prove its author
Quick revision
- SOAP vs REST; HTTP request and JSON response.
- HttpURLConnection on a background thread; OkHttp, Retrofit, Volley.
- XML vs JSON; JSONObject, JSONArray, XmlPullParser.
- Started and bound services; life cycle; foreground services; WorkManager.
- Release checklist; keystore and Play App Signing; store graphics; Play Console tracks.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Distinguish SOAP and REST.
- Q2.Why must network calls run off the main thread?
- Q3.Distinguish XML and JSON.
- Q4.Distinguish started and bound services.
- Q5.What is a foreground service?
- Q6.Why must an app be signed?
Long-answer questions
- Q1.Explain how an Android app consumes a REST web service.
- Q2.Explain XML and JSON parsing in Android.
- Q3.Explain the service life cycle and foreground services.
- Q4.Explain the steps to sign and publish an app on Google Play.
Stuck on this unit?
Message SBS on WhatsApp for help with Mobile Application Development, or to ask about studying B.Sc IT at Synetic.
