0% found this document useful (0 votes)
2 views8 pages

REST API

The document outlines a mobile application development assignment focused on implementing a bound service for background data fetching from a REST API using Retrofit. It details the architecture, including the PostService that retrieves posts from JSONPlaceholder and communicates results back to the MainActivity, ensuring a responsive UI. The provided code snippets illustrate the service, model, and main activity components necessary for the application functionality.

Uploaded by

hamzaayyub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

REST API

The document outlines a mobile application development assignment focused on implementing a bound service for background data fetching from a REST API using Retrofit. It details the architecture, including the PostService that retrieves posts from JSONPlaceholder and communicates results back to the MainActivity, ensuring a responsive UI. The provided code snippets illustrate the service, model, and main activity components necessary for the application functionality.

Uploaded by

hamzaayyub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

a

Mobile Application
Development
Assignment # 3

Masooma Qaisar
Rcf- 30072
Explanation:

In this task, I used a bound service to handle background data fetching from a REST API and
communicate the result back to the main activity. The service is responsible for managing
network operations, specifically calling the JSONPlaceholder API to retrieve a post. I chose
Retrofit as the HTTP client to streamline the process of making network requests and handling
responses.

The main activity binds to the service using bindService(). Once the data is fetched from the
API, the service uses a Binder to pass the result back to the activity. The activity then updates the
user interface with the retrieved data. A callback method ensures smooth communication
between the service and the activity.

By offloading data fetching to a background service, the main thread remains free to handle UI
updates, preventing UI freezes or Application Not Responding (ANR) errors. Retrofit is utilized
to handle the network request efficiently, and I implemented proper lifecycle management for
binding and unbinding the service.

This architecture ensures that the app remains responsive by efficiently managing background
tasks, enhancing the user experience and overall performance.
Code:

Post.java:

package com.example.rest_api.model;

public class Post {


private int userId;
private int id;
private String title;
private String body;

public int getUserId() { return userId; }


public int getId() { return id; }
public String getTitle() { return title; }
public String getBody() { return body; }
}

PostService.java:
package com.example.rest_api.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.example.rest_api.ApiService;
import com.example.rest_api.model.Post;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Interceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostService extends Service {

private final IBinder binder = new LocalBinder();


private ApiService apiService;

public interface PostCallback {


void onPostReceived(Post post);
void onError(String error);
}

public class LocalBinder extends Binder {


public PostService getService() {
return PostService.this;
}
}

@Override
public void onCreate() {
super.onCreate();

OkHttpClient client = new OkHttpClient.Builder()


.addInterceptor(chain -> {
Request request = chain.request().newBuilder()
.addHeader("Accept-Language", "en")
.build();

Log.d("PostService", "Request URL: " + request.url());


Log.d("PostService", "Request Headers: " +
request.headers());

return chain.proceed(request);
})
.build();

Retrofit retrofit = new Retrofit.Builder()


.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();

apiService = retrofit.create(ApiService.class);
}

public void fetchPost(int postId, PostCallback callback) {


Call<Post> call = apiService.getPost(postId);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (response.isSuccessful() && response.body() != null) {
Log.d("PostService", "Fetched Post Title: " +
response.body().getTitle());
Log.d("PostService", "Fetched Post Body: " +
response.body().getBody());

callback.onPostReceived(response.body());
} else {
callback.onError("Failed to load post");
}
}

@Override
public void onFailure(Call<Post> call, Throwable t) {
callback.onError(t.getMessage());
}
});
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}
}

API Service:
package com.example.rest_api;

import com.example.rest_api.model.Post;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface ApiService {


@GET("posts/{id}")
Call<Post> getPost(@Path("id") int id);
}

MainActivity.java:
package com.example.rest_api;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rest_api.model.Post;
import com.example.rest_api.service.PostService;

public class MainActivity extends AppCompatActivity {

private PostService postService;


private boolean isBound = false;

private TextView postTitle;


private TextView postBody;
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
PostService.LocalBinder binder = (PostService.LocalBinder)
service;
postService = binder.getService();
isBound = true;

fetchAndDisplayPost();
}

@Override
public void onServiceDisconnected(ComponentName name) {
isBound = false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

postTitle = findViewById(R.id.postTitle);
postBody = findViewById(R.id.postBody);
Intent intent = new Intent(this, PostService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);

private void fetchAndDisplayPost() {


postService.fetchPost(1, new PostService.PostCallback() {
@Override
public void onPostReceived(Post post) {
runOnUiThread(() -> {
postTitle.setText(post.getTitle());
postBody.setText(post.getBody());
});
}

@Override
public void onError(String error) {
runOnUiThread(() -> {
postTitle.setText("Error");
postBody.setText(error);
});
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
if (isBound) {
unbindService(connection);
isBound = false;
}
}
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:background="@android:color/white">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="REST API"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#6200EE"
android:gravity="center"
android:paddingBottom="16dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/postTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title Section"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:paddingBottom="12dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/postBody"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Body Section"
android:textSize="16sp"
android:textColor="#444444"
android:lineSpacingExtra="6dp"
android:paddingBottom="16dp"
android:layout_marginTop="10dp" />

</LinearLayout>

You might also like