Reputation: 45
I'm trying to make RecyclerView from API that my friend created.
But suddenly i have this problem, help me to solve this :'(
I don't know what this mean, cuz this is my first time usin' retrofit for RecyclerView Data. I really appreciate your help.
/home/ardityoc/AndroidStudioProjects/HomeCare/app/src/main/java/com/humic/android/homecare/ui/user/home/HomeFragment.java:83: error: incompatible types: <anonymous Callback<ServiceResponse>> cannot be converted to Callback<Service>
ServiceApiService.getAllServicesUserRequest("Bearer " + loadToken()).enqueue(new Callback<ServiceResponse>() {
^
This is my HomeFragment.java
package com.humic.android.homecare.ui.user.home;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.humic.android.homecare.R;
import com.humic.android.homecare.activity.user.MainActivity;
import com.humic.android.homecare.adapter.RecyclerViewServiceAdapter;
import com.humic.android.homecare.model.Service;
import com.humic.android.homecare.rest.ApiClient;
import com.humic.android.homecare.rest.ApiInterface;
import com.humic.android.homecare.rest.service.ServiceResponse;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static android.content.Context.MODE_PRIVATE;
import static com.humic.android.homecare.activity.LoginActivity.userToken;
import static com.humic.android.homecare.activity.user.CurrentUserActivity.userName;
import static com.humic.android.homecare.utils.SharedPreference.SHARED_PREFS_TOKEN;
import static com.humic.android.homecare.utils.SharedPreference.SHARED_PREFS_USER;
public class HomeFragment extends Fragment {
String nameCache, tokenCache;
TextView UserName;
RecyclerView ServiceRecyclerView;
RecyclerViewServiceAdapter ServiceAdapter;
List<Service> ServiceList;
Context ServiceContext;
ApiInterface ServiceApiService;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
ServiceContext = getContext();
ServiceApiService = ApiClient.getAPIService();
initComponents(root);
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ServiceRecyclerView = (RecyclerView) getView().findViewById(R.id.list_home_service);
ServiceAdapter = new RecyclerViewServiceAdapter(getActivity(),ServiceList);
ServiceRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
private void requestServices() {
ServiceApiService.getAllServicesUserRequest("Bearer " + loadToken()).enqueue(new Callback<ServiceResponse>() {
@Override
public void onResponse(Call<ServiceResponse> call, Response<ServiceResponse> response) {
if (response.isSuccessful()){
Log.i("debug", "onResponse: Berhasil");
try {
ServiceResponse jsonResponse = response.body();
JSONObject jsonRESULTS = new JSONObject(jsonResponse.toString());
if (jsonRESULTS.getJSONObject("meta").getString("status").equals("success")){
// ServiceList = new ArrayList<>(jsonResponse.getServiceResponse());
ServiceList = jsonResponse.getServiceResponse();
ServiceRecyclerView.setAdapter(ServiceAdapter);
} else {
String error_message = jsonRESULTS.getString("error_msg");
Toast.makeText(ServiceContext, error_message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException | IOException e) { //Error di sini
e.printStackTrace();
}
} else {
Log.i("debug", "onResponse: Tidak Berhasil");
}
}
@Override
public void onFailure(Call<ServiceResponse> call, Throwable t) {
Log.e("debug", "onFailure: ERROR > " + t.getMessage());
Toast.makeText(ServiceContext, "Koneksi Internet Bermasalah", Toast.LENGTH_SHORT).show();
}
});
}
private void initComponents(View root){
getViewId(root);
UserName.setText(loadCurrentUserName());
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) getActivity().findViewById(R.id.toolbar_layout);
}
private void getViewId(View root){
UserName = root.findViewById(R.id.user_name);
}
private String loadToken(){
final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(SHARED_PREFS_TOKEN, MODE_PRIVATE);
tokenCache = sharedPreferences.getString(userToken, "");
return tokenCache;
}
private String loadCurrentUserName(){
final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(SHARED_PREFS_USER, MODE_PRIVATE);
nameCache = sharedPreferences.getString(userName, "");
return nameCache;
}
}
^
This is JSON Response from my friend.
{
"meta": {
"code": 200,
"status": "success",
"message": "successfully load data"
},
"data": [
{
"id": 1,
"name": "Pemasangan infus",
"information": "pemasangan kateter untuk kegiatan medis",
"image": "https://via.placeholder.com/300",
"price": 120000,
"created_at": "2021-07-29T01:35:43.000000Z",
"updated_at": "2021-07-29T01:35:43.000000Z"
},
{
"id": 2,
"name": "Pemasangan infus",
"information": "pemasangan infus untuk kegiatan medis",
"image": "https://via.placeholder.com/300",
"price": 80000,
"created_at": "2021-07-29T01:35:43.000000Z",
"updated_at": "2021-07-29T01:35:43.000000Z"
},
{
"id": 3,
"name": "Perawatan luka jahitan",
"information": "perawatan luka pasca operasi, umumnya luka jahitan",
"image": "https://via.placeholder.com/300",
"price": 70000,
"created_at": "2021-07-29T01:35:43.000000Z",
"updated_at": "2021-07-29T01:35:43.000000Z"
},
{
"id": 4,
"name": "suntik vaksin titan",
"information": "melakukan suntik untuk mendapatkan kekuatan titan, kekuatan titan yang dihasilkan bersifat acak.",
"image": "http://149.28.136.231/storage/services/Titan_injection_(Anime).webp",
"price": 120000,
"created_at": "2021-07-29T04:11:43.000000Z",
"updated_at": "2021-07-29T10:17:05.000000Z"
}
]
}
This is my Service.java
package com.humic.android.homecare.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Service {
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("information")
@Expose
private String information ;
@SerializedName("image")
@Expose
private String image ;
@SerializedName("price")
@Expose
private int price ;
public Service() {}
public Service(int Id, String Name, String Information, String Image, int Price) {
this.id = Id;
this.name = Name;
this.information = Information;
this.image = Image;
this.price = Price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getInformation() {
return information;
}
public String getImage() {
return image;
}
public int getPrice() {
return price;
}
public void setId(int Id) {
this.id = Id;
}
public void setName(String Name) {
this.name = Name;
}
public void setInformation(String Information) {
this.information = Information;
}
public void setImage(String Image) {
this.image = Image;
}
public void setPrice(int Price) {
this.price = Price;
}
}
This is my ServiceResponse.java
package com.humic.android.homecare.rest.service;
import com.google.gson.annotations.SerializedName;
import com.humic.android.homecare.model.Service;
import java.util.ArrayList;
public class ServiceResponse {
@SerializedName("data")
ArrayList<Service> ServiceResponse;
public ArrayList<Service> getServiceResponse() {
return ServiceResponse;
}
public void setServiceResponse(ArrayList<Service> ServiceResponse) {
this.ServiceResponse = ServiceResponse;
}
}
This is my RecyclerViewServiceAdapter.java
package com.humic.android.homecare.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.humic.android.homecare.R;
import com.humic.android.homecare.activity.user.ServiceActivity;
import com.humic.android.homecare.model.Service;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class RecyclerViewServiceAdapter extends RecyclerView.Adapter<RecyclerViewServiceAdapter.ServiceViewHolder> {
private Context serviceContext ;
private List<Service> serviceData ;
public RecyclerViewServiceAdapter(Context serviceContext, List<Service> serviceData) {
this.serviceContext = serviceContext;
this.serviceData = serviceData;
}
@NotNull
@Override
public ServiceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(serviceContext);
view = mInflater.inflate(R.layout.item_home_service,parent,false);
return new ServiceViewHolder(view);
}
@Override
public void onBindViewHolder(ServiceViewHolder holder, final int position) {
holder.serviceName.setText(serviceData.get(position).getName());
// holder.serviceThumbnail.setImageResource(serviceData.get(position).getImage());
holder.serviceCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(serviceContext, ServiceActivity.class);
intent.putExtra("name",serviceData.get(position).getName());
intent.putExtra("information",serviceData.get(position).getInformation());
intent.putExtra("price",serviceData.get(position).getPrice());
// intent.putExtra("image",serviceData.get(position).getImage());
serviceContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return serviceData.size();
}
public static class ServiceViewHolder extends RecyclerView.ViewHolder {
TextView serviceName;
// ImageView serviceImage;
CardView serviceCardView ;
public ServiceViewHolder(View itemView) {
super(itemView);
serviceName = (TextView) itemView.findViewById(R.id.list_home_service_title) ;
// serviceImage = (ImageView) itemView.findViewById(R.id.list_home_service_thumbnail);
serviceCardView = (CardView) itemView.findViewById(R.id.list_home_service_cardview);
}
}
}
This is my ApiInterface.java
package com.humic.android.homecare.rest;
import com.humic.android.homecare.model.Service;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface ApiInterface {
// Services
@GET("services")
Call<Service> getAllServicesUserRequest(@Header("Authorization") String token);
}
Upvotes: 1
Views: 694
Reputation: 1463
The signature of your getAllServicesUserRequest
is incorrect. According to the provided json, it should receive ServiceResponse
, also when you enqueue the call you also defined there that you expect ServiceResponse
. So updating the signature of getAllServicesUserRequest
to Call<ServiceResponse>
should fix your issue:
@GET("services")
Call<ServiceResponse> getAllServicesUserRequest(@Header("Authorization") String token);
Upvotes: 1