Unit 3 of 4 · B.Sc IT Sem 6

Unit 3: Network communication and publishing

Mobile Application Development notes · PTU syllabus (BSIT601)

3 min read8 topics10 exam questions
On this page
  1. Unit summary
  2. Web services
  3. HTTP client
  4. XML and JSON
  5. Services and the service life cycle
  6. Foreground services
  7. Preparing for publishing
  8. Signing the app and preparing graphics
  9. Publishing to Google Play
  10. Key terms
  11. Quick revision
  12. Important questions

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
ProcessPublishing an Android app
  1. 1

    Test on devices and emulators

  2. 2

    Set version and remove debug code

  3. 3

    Sign the app

    Generate a release key

  4. 4

    Prepare graphics

    Icon, screenshots

  5. 5

    Upload to Google Play Console

  6. 6

    Release and monitor

1

Topic 1

Web services

ComparisonWeb service styles
SOAP
REST

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.
2

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.

3

Topic 3

XML and JSON

ComparisonXML and JSON
XML
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();
}
4

Topic 4

Services and the service life cycle

ComparisonStarted and bound services
Started service
Bound service

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

ProcessStarted service life cycle
  1. 1

    startService() called

  2. 2

    onCreate() (first time only)

  3. 3

    onStartCommand() — do the work on a background thread

  4. 4

    Service running

  5. 5

    stopSelf() or stopService()

  6. 6

    onDestroy()

5

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.
6

Topic 6

Preparing for publishing

ProcessRelease checklist
  1. 1

    Remove logging and debug flags

  2. 2

    Set versionCode and versionName in Gradle

  3. 3

    Choose minSdk and targetSdk (Play requires a recent target API)

  4. 4

    Shrink and obfuscate with R8

  5. 5

    Test on several devices and screen sizes

  6. 6

    Write a privacy policy and data safety details

  7. 7

    Build a signed App Bundle (.aab)

7

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.
Key termsStore graphics
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
8

Topic 8

Publishing to Google Play

ProcessPublishing steps
  1. 1

    Create a Google Play developer account (one-time fee)

  2. 2

    Create the app and fill in store listing — title, description, graphics

  3. 3

    Complete content rating, target audience, data safety and privacy policy

  4. 4

    Upload the signed App Bundle to a testing track (internal, closed, open)

  5. 5

    Fix issues reported by pre-launch reports

  6. 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

  1. Q1.Distinguish SOAP and REST.
  2. Q2.Why must network calls run off the main thread?
  3. Q3.Distinguish XML and JSON.
  4. Q4.Distinguish started and bound services.
  5. Q5.What is a foreground service?
  6. Q6.Why must an app be signed?

Long-answer questions

  1. Q1.Explain how an Android app consumes a REST web service.
  2. Q2.Explain XML and JSON parsing in Android.
  3. Q3.Explain the service life cycle and foreground services.
  4. 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.

WhatsApp us