REST API
REST API
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;
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;
@Override
public void onCreate() {
super.onCreate();
return chain.proceed(request);
})
.build();
apiService = retrofit.create(ApiService.class);
}
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;
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;
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);
@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>