0

I have query related to multiple api calls . I need to show data's to a recyclerview from 2 different API responses. How to combine all responses from first api and selected fields from 2nd API responses

Eg : Api1 - [{ "item_id : "1", "item_name" : "soap" }]

Api2- [{ "item_id : "1", "item_price" : "10" }]

Output like - Api1 - [{ "item_id : "1", "item_name" : "soap", "item_price" : "10" }]

My Android Classes:

Model Class

data class UsersData(
    @SerializedName("avatar")
    val avatar: String,
    @SerializedName("id")
    val id: Int,
    @SerializedName("name")
    val name: String,
    )
    enter code here

data class PostData(
    @SerializedName("id")
    val id: Int,
    @SerializedName("body")
    val body: String,
    @SerializedName("title")
    val title: String,
)
data class SocialMediaData(
    @SerializedName("avatar")
    var avatar: String="default",
    @SerializedName("id")
    var id: Int=0,
    @SerializedName("name")
    var name: String="name",
    @SerializedName("body")
    var body: String="body",
    @SerializedName("title")
    var title: String="title",
)

This is my viewmodel function

class ParallelNetworkCallsViewModel(
    private val apiHelper: ApiHelper,

    ) : ViewModel() {

    private val usersList = MutableLiveData<Resource<List<UsersData>>>()

    private val postList = MutableLiveData<Resource<List<PostData>>>()

    private var allDatasFromApi = MutableLiveData<Resource<List<SocialMediaData>>>()
    
    private val   resultModel = SocialMediaData()

    init {
        fetchUsers()
    }

    private fun fetchUsers() {

        val apikey = "891B1ACC2262F7268DC688D48178C9D9"
        viewModelScope.launch {

            allDatasFromApi.postValue(Resource.loading(null))
            try {
                // coroutineScope is needed, else in case of any network error, it will crash
                coroutineScope {
                    val usersFromApiDeferred = async { apiHelper.getUsersData(apikey) }
                    val postFromApiDeferred = async { apiHelper.getPostsData(apikey) }

                    val usersFromApi = usersFromApiDeferred.await()
                    val postFromApi = postFromApiDeferred.await()


                    Log.i("hfhf",""+allDatasFromApi)

                    for (l1 in usersFromApi) {
                        for (l2 in postFromApi) {
                            if (l1.id == l2.id) {

                                resultModel.name = l1.name
                                resultModel.avatar = l1.avatar
                                resultModel.title= l2.title
                                resultModel.name = l2.body


                                allDatasFromApi.postValue(resultModel)

                            }
                        }
                    }


                }
            } catch (e: Exception) {
                usersList.postValue(Resource.error("Something Went Wrong", null))
                postList.postValue(Resource.error("Something Went Wrong", null))
            }
        }
    }




    fun getUsers(): LiveData<Resource<List<SocialMediaData>>> {
        return allDatasFromApi
    }

Shows error in this line

allUsersFromApi.postValue(resultModel)

arraylist.post() is not working.

shows:

Type mismatch. Required: Resource! Found: SocialMediaData

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
  • First of all, if you want to merge 2 API's data together then you will need to have same response from API and then add it to same list and then show it. Otherwise what you can do is, you can loop through the data that you receive from API 2 and add the required data to the list which comes from API 1. – Karan Mehta Sep 07 '22 at 04:35
  • 1
    You asked this before: https://stackoverflow.com/questions/73589753/show-datas-to-a-recyclerview-from-2-different-apis-how-to-combine-responses-fr – blackapps Sep 07 '22 at 05:20
  • `Shows error in this line allUsersFromApi.postValue(resultModel)` You told that before. And again you are not telling us which error. And when do you see that error. Also you are not telling us what that statement should do. – blackapps Sep 07 '22 at 05:22
  • `[{ "item_id : "1", "item_name" : "soap" }]` That does not match your UserData class. – blackapps Sep 07 '22 at 05:23
  • Does this answer your question? [show data's to a recyclerview from 2 different APIs. How to combine responses from 1st api and selected fields from 2nd API responses](https://stackoverflow.com/questions/73589753/show-datas-to-a-recyclerview-from-2-different-apis-how-to-combine-responses-fr) – Sven Eberth Jan 30 '23 at 20:59

0 Answers0