I am trying to fetch data from server and showing it in recycler view.I am using retrofit library and RxJava2 but its unable to fetch data from server.
Its showing following line in LogCat:
E/RecyclerView: No adapter attached; skipping layout
Response form the server:
[
{
"term_id": "4",
"name": "Entertainment"
},
{
"term_id": "5",
"name": "Tech & Gadgets"
},
{
"term_id": "6",
"name": "Sports"
},
{
"term_id": "7",
"name": "Health and Fitness Tips"
}
]
Below is my code:
RetrofitClient.java
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getInstance(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(22,TimeUnit.SECONDS)
.readTimeout(22, TimeUnit.SECONDS)
.writeTimeout(22, TimeUnit.SECONDS)
.build();
if(retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl("https://www.flypped.com/api/")
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
}
ApiService.class
public interface ApiService {
#GET("Categoery_api")
Observable<List<Model>> getData();
}
Model.java
public class Model {
#SerializedName("catId")
#Expose
String catId;
#SerializedName("catName")
#Expose
String catName;
public Model(){
}
public Model(String catId, String catName) {
this.catId = catId;
this.catName = catName;
}
public String getCatId() {
return catId;
}
public void setCatId(String catId) {
this.catId = catId;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
}
MainActivity.java
private void fetchData(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService myApi = retrofit.create(ApiService.class);
myApi.getData().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Model>>() {
#Override
public void onSubscribe(Disposable d) {
d.dispose();
}
#Override
public void onNext(List<Model> models) {
if(models.size() > 0){
progress.setVisibility(View.INVISIBLE);
adapter = new PostAdapter(getApplicationContext(),list);
recycler.setAdapter(adapter);
}
}
#Override
public void onError(Throwable e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete() {
}
});
}
PostAdapter.java
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
List<Model> list = new ArrayList<>();
Context context;
public PostAdapter(Context context,List<Model> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public PostAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_row,parent,false);
ViewHolder view = new ViewHolder(v);
return view;
}
#Override
public void onBindViewHolder(#NonNull PostAdapter.ViewHolder holder, int position) {
Model model = list.get(position);
holder.catName.setText(model.getCatName());
holder.catId.setText(model.getCatId());
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView catId,catName,comp,titl;
public ViewHolder(#NonNull View itemView) {
super(itemView);
catId = itemView.findViewById(R.id.catId);
catName = itemView.findViewById(R.id.catName);
}
}
}
Someone please let me know what I am doing wrong any help would be appreciated.
THANKS
You are calling d.dispose(); in onSubscribe which will dispose the resource and result so remove dispose call as
public void onSubscribe(Disposable d) {
//d.dispose(); remove it
}
you can move dispose in onDestroy to free up resources(JIC request is still running) when activity(or fragment) is going to be removed from memory and make sure you have layout manager set on recycler view.
Related
I'm stuck somewhere in between getting json array inside a object setting it to recylerview, I have to send a key to fetch data and getting response in json array inside a object. Didn't understand solutions got here, so I'm here if Somebody can help mre understand.
////Main class
public class today_doctors extends AppCompatActivity {
viewModel_doc listViewModel;
List<model_doc> datalist;
todayDoctorAdapter adapter;
TextView clinic_name;
RecyclerView recview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_today_doctors);
recview=findViewById(R.id.rv);
recview.setLayoutManager(new LinearLayoutManager(this));
recview.addItemDecoration(new DividerItemDecoration(this, 0));
adapter=new todayDoctorAdapter(datalist);
recview.setAdapter(adapter);
clinic_name=findViewById(R.id.tvDrName);
process();
}
public void process(){
listViewModel= new ViewModelProvider(this).get(viewModel_doc.class);
listViewModel.getDatalistObserver ().observe(this, new Observer<List<model_doc>>() {
#Override
public void onChanged(List<model_doc> Models) {
if(Models!=null) {
datalist= Models;
adapter.updateList(Models);
}
else
{
recview.setVisibility(View.GONE);
Toast.makeText(today_doctors.this, "No data recieved", Toast.LENGTH_SHORT).show();
}
}
});
listViewModel.makeApiCall();
}
//////////Viewmodel class
public class viewModel_doc extends ViewModel
{
private MutableLiveData<List<model_doc>> datalist2;
public viewModel_doc(){
datalist2=new MutableLiveData<>();
}
public MutableLiveData<List<model_doc>> getDatalistObserver()
{
return datalist2;
}
public void makeApiCall()
{
apiSet apiServices= apiController.getInstance().getApi();
Call<List<model_doc>> calldoc=apiServices.getList2("ll0004");
calldoc.enqueue(new Callback<List<model_doc>>() {
#Override
public void onResponse(Call<List<model_doc>> call, Response<List<model_doc>> response) {
datalist2.postValue(response.body());
}
#Override
public void onFailure(Call<List<model_doc>> call, Throwable t) {
datalist2.postValue(null);
Log.e("Error :",t.getMessage());
}
});
}
}
///////////////////main model
public class model_doc {
String status,msg;
ArrayList<Data> data;
public model_doc(String status, String msg, ArrayList<Data> data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public String getStatus() {
return status;
}
public String getMsg() {
return msg;
}
public ArrayList<Data> getData() {
return data;
}
public static class Data {
String doctor_id;
String doctor_name;
String proposed_date;
String date;
// Getters setters
public Data(String doctor_id, String doctor_name, String proposed_date, String date) {
this.doctor_id = doctor_id;
this.doctor_name = doctor_name;
this.proposed_date = proposed_date;
this.date = date;
}
public String getDoctor_id() {
return doctor_id;
}
public String getDoctor_name() {
return doctor_name;
}
public String getProposed_date() {
return proposed_date;
}
public String getDate() {
return date;
}
}
}
////api interface
#FormUrlEncoded
#POST("doctor_list")
Call<List<model_doc>>getList2(
#Field("clinic_id") String clinic_id
);
//////////////////controller
public class apiController {
private static final String url="https://xyz.in/api/";
private static apiController clientObject;
private static Retrofit retrofit;
apiController()
{
retrofit=new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized apiController getInstance(){
if(clientObject==null)
clientObject=new apiController();
return clientObject;
}
public apiSet getApi(){
return retrofit.create(apiSet.class);
}
}
////////////recycler adapter, made some changes in main model for nested data could handle it to adapter
public class todayDoctorAdapter extends RecyclerView.Adapter<todayDoctorAdapter.viewholder>
{
List<model_doc> datalist2;
public todayDoctorAdapter(List<model_doc> list){
this.datalist2=list;}
public void updateList(List<model_doc>list){
this.datalist2=list;
notifyDataSetChanged();
}
#NonNull
#Override
public todayDoctorAdapter.viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_doctors,parent,false);
return new todayDoctorAdapter.viewholder(view);
}
#Override
public void onBindViewHolder(#NonNull todayDoctorAdapter.viewholder holder, int position){
holder.doctor_name.setText(String.format("Dr.%s", datalist2.get(position).getDoctor_app()));
holder.proposed_date.setText(String.format("Apt: %s","TODAY"));
String doc=datalist2.get(position).getDoctor_app();
holder.cv.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intent = new Intent(arg0.getContext(), PatientsUnderDoctor.class);
intent.putExtra("doc_name", doc);
arg0.getContext().startActivity(intent);
}
});
}
#Override
public int getItemCount(){
if (this.datalist2!=null){
return this.datalist2.size();
}
return 0;
}
public static class viewholder extends RecyclerView.ViewHolder{
TextView doctor_name,proposed_date;
CardView cv;
public viewholder(#NonNull View itemView) {
super(itemView);
doctor_name=itemView.findViewById(R.id.drName);
proposed_date=itemView.findViewById(R.id.apptDate);
cv=itemView.findViewById(R.id.cv_patient);
}
}
}
//////////////////json data look like this
{
"status": "success",
"msg": "Found",
"data": [
{
"doc_code": "jhjh0001",
"app_date": "2022-09-13",
"doc_name": "kjk",
"count": 1
}
]
}
I am trying to learn RxJava and was implementing a simple app where I am loading a list of posts from JSONPlaceholder and for each post I am loading the comments by calling another api and updating the posts adapter. The problem is onNext() method of the post observable is getting called only once and only one posts comments is getting loaded.
Here is my activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView rv;
private PostsAdapter adapter;
private CompositeDisposable disposables = new CompositeDisposable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this,
RecyclerView.VERTICAL, false));
adapter = new PostsAdapter(this);
rv.setAdapter(adapter);
getPostObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function<Post, ObservableSource<Post>>() {
#Override
public ObservableSource<Post> apply(Post post) throws Exception {
return getCommentsObservable(post);
}
}).subscribe(new Observer<Post>() {
#Override
public void onSubscribe(Disposable d) {
disposables.add(d);
}
#Override
public void onNext(Post post) {
Log.d(TAG, "onNext: called");
updatePost(post);
}
#Override
public void onError(Throwable e) {
Log.d(TAG, "onError: ", e);
}
#Override
public void onComplete() {
Log.d(TAG, "onComplete: called");
}
});
}
private void updatePost(Post post) {
adapter.updatePost(post);
}
private Observable<Post> getPostObservable() {
return ApiClient.getApi()
.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap((Function<List<Post>, ObservableSource<Post>>) posts -> {
adapter.setPosts(posts);
return Observable.fromIterable(posts)
.subscribeOn(Schedulers.io());
});
}
private Observable<Post> getCommentsObservable(final Post post) {
return ApiClient.getApi()
.getComments(post.getId())
.map(new Function<List<Comment>, Post>() {
#Override
public Post apply(List<Comment> comments) throws Exception {
int delay = (new Random().nextInt(5) + 1) * 1000;
Thread.sleep(delay);
post.setComments(comments);
return post;
}
})
.subscribeOn(Schedulers.io());
}
#Override
protected void onDestroy() {
super.onDestroy();
disposables.clear();
}
}
Here is the Adapter
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.ViewHolder> {
private final String TAG = "nexa_" + this.getClass().getSimpleName();
private Context context;
private List<Post> dataList = new ArrayList<>();
public PostsAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_posts, parent, false)
);
}
public void setPosts(List<Post> posts) {
this.dataList = posts;
notifyDataSetChanged();
}
public void updatePost(Post post) {
dataList.set(dataList.indexOf(post), post);
notifyItemChanged(dataList.indexOf(post));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if (dataList.isEmpty())
return;
holder.bind(dataList.get(position));
}
#Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvNumber, tvDesc;
ProgressBar progressBar;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvNumber = itemView.findViewById(R.id.tv_number);
tvDesc = itemView.findViewById(R.id.tv_desc);
progressBar = itemView.findViewById(R.id.progressbar);
}
public void bind(Post post) {
tvDesc.setText(post.getBody());
if (post.getComments() == null) {
toggleProgressbar(true);
tvNumber.setText("");
} else {
toggleProgressbar(false);
tvNumber.setText(String.valueOf(post.getComments().size()));
}
}
void toggleProgressbar(boolean show) {
if (show) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
}
}
}
ApiClient class
public class ApiClient {
public static final String BASE_URL = "https://jsonplaceholder.typicode.com/";
private static Retrofit retrofit = null;
public static ApiInterface getApi() {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
.tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA)
.build();
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectionSpecs(Collections.singletonList(spec))
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "");
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit.create(ApiInterface.class);
}
}
ApiInterface class
public interface ApiInterface {
#GET("posts")
Observable<List<Post>> getPosts();
#GET("posts/{id}/comments")
Observable<List<Comment>> getComments(
#Path("id") int id
);
}
The problem was i was calling getCommentsObservable() which is a network call from background thread. I added observeOn(AndroidSchedulers.mainThread()) and it is working now.
private Observable<Post> getCommentsObservable(final Post post) {
return ApiClient.getApi()
.getComments(post.getId())
.map(new Function<List<Comment>, Post>() {
#Override
public Post apply(List<Comment> comments) throws Exception {
int delay = (new Random().nextInt(5) + 1) * 1000;
Thread.sleep(delay);
post.setComments(comments);
return post;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); // ADDED THIS LINE
}
I want to fetch some data from the server by using the retrofitlibrary on RecyclerView but the data is not showing on RecycleView. I read lots of answers but did not solve it yet so please solve my problem thank you in advance.
Below is my JSON Data
{
"item": [
{
"Item_Id": "1",
"Item_Name": "Item Name 1",
"Item_Description": "Item Name 1 Description",
"Item_Price": "330.00",
"Discount": "10",
"Item_Image": "http://192.168.1.5/easyshop/Items_Image/xyz.jpg",
"First_Name": "abc",
"Last_Name": "xyz",
"Mobile": "**********"
},
{
"Item_Id": "2",
"Item_Name": "Item Name 2",
"Item_Description": "Item Name 2 Description",
"Item_Price": "40.00",
"Discount": "30",
"Item_Image": "http://192.168.1.5/easyshop/Items_Image/xyz1.jpg",
"First_Name": "def",
"Last_Name": "uvw",
"Mobile": "**********"
}
]
}
API Client class where I have used the base URL, GSON and retrofit object
public class ApiClient {
public static final String BASE_URL = "http://192.168.1.5/easyshop/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100,TimeUnit.SECONDS)
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
Interface
public interface APIService {
#FormUrlEncoded
#POST("GetItem.php")
Call<ItemArray> getItem(#Field("Retailer_Id")int Retailer_Id);
}
When I put JSON data in www.jsonschema2pojo.org it generates two files which are following as below
public class ItemArray {
#SerializedName("item")
#Expose
private List<ItemList> item = null;
public List<ItemList> getItem() {
return item;
}
public void setItem(List<ItemList> item) {
this.item = item;
}
}
getter Setter Class
public class ItemList {
#SerializedName("Item_Id")
#Expose
private String itemId;
#SerializedName("Item_Name")
#Expose
private String itemName;
#SerializedName("Item_Description")
#Expose
private String itemDescription;
#SerializedName("Item_Price")
#Expose
private String itemPrice;
#SerializedName("Discount")
#Expose
private String discount;
#SerializedName("Item_Image")
#Expose
private String itemImage;
#SerializedName("First_Name")
#Expose
private String firstName;
#SerializedName("Last_Name")
#Expose
private String lastName;
#SerializedName("Mobile")
#Expose
private String mobile;
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) { this.itemDescription = itemDescription; }
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getItemImage() {
return itemImage;
}
public void setItemImage(String itemImage) {
this.itemImage = itemImage;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
Adapter Class
public class Item_Card_Adapter extends RecyclerView.Adapter<Item_Card_Adapter.ViewHolder> {
//List to store all items
List<ItemList> items;
//Constructor of this class
public Item_Card_Adapter(List<ItemList> items){
super();
//Getting all items
this.items = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_layout,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(Item_Card_Adapter.ViewHolder holder, int position) {
holder.itemName.setText(items.get(position).getItemName());
holder.itemDesc.setText(items.get(position).getItemDescription());
holder.itemPrice.setText(items.get(position).getItemPrice());
}
#Override
public int getItemCount() {return items.size();}
class ViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.ItemName) TextView itemName;
#Bind(R.id.ItemDesc) TextView itemDesc;
#Bind(R.id.ItemPrice) TextView itemPrice;
#Bind(R.id.cardView) CardView cardView;
//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Item_Get_Activity Activity class
public class Item_Get_Activity extends AppCompatActivity {
#Bind(R.id.recyclerrView) RecyclerView recyclerView;
#Bind(R.id.progressBar) ProgressBar progressBar;
private List<ItemList> data;
private Item_Card_Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_get_activity);
ButterKnife.bind(this);
initViews();
}
private void initViews() {
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
getDataFromServer();
}
public void getDataFromServer() {
int i = 1;
APIService service = ApiClient.getClient().create(APIService.class);
Call<ItemArray> userCall = service.getItem(i);
userCall.enqueue(new Callback<ItemArray>() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onResponse(Call<ItemArray> call, Response<ItemArray> response) {
if(response.isSuccessful()){
progressBar.setVisibility(View.GONE);
data = response.body().getItem();
adapter = new Item_Card_Adapter(data);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onFailure(Call<ItemArray> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
}
You need to change your ApiServices Interface like this
public interface APIService {
#FormUrlEncoded
#POST("GetItem.php")
Call<ItemArray> getItem(#Field("Retailer_Id")int Retailer_Id);
}
Then in your Activity update the calling method
public void getDataFromServer() {
int i = 1;
APIService service = ApiClient.getClient().create(APIService.class);
Call<ItemArray> userCall = service.getItem(i);
userCall.enqueue(new Callback<ItemArray>() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onResponse(Call<ItemArray> call, Response<ItemArray> response) {
if(response.isSuccessful()){
progressBar.setVisibility(View.GONE);
data = response.body().getItem();
adapter = new Item_Card_Adapter(data);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onFailure(Call<List<ItemList>> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
Since your api response contain the "list" inside a Json object. You need a class that has a List variable. So retrofit will parse the response to ItemArray successfully
Update
The demo code link https://github.com/Siddharth-Dev/RetrofitDemo
This has your code and a working API code too.
I am trying to make a call to this api and am having difficulty as the response.body() is returning null.
http://demo.museum.vebrary.vn/api/stuff/getall
I want to get stuff name of list and show to my recyclerview.
My model:
public class SOAnswersResponse {
#SerializedName("StuffModels")
#Expose
private List<StuffModel> stuffModels = null;
public List<StuffModel> getStuffModels() {
return stuffModels;
}
public void setStuffModels(List<StuffModel> stuffModels) {
this.stuffModels = stuffModels;
}
and
public class StuffModel {
#SerializedName("STUFFID")
#Expose
private Integer sTUFFID;
#SerializedName("STUFFCODE")
#Expose
private String sTUFFCODE;
#SerializedName("STUFFNAME")
#Expose
private String sTUFFNAME;
#SerializedName("STUFFNOTE")
#Expose
private String sTUFFNOTE;
#SerializedName("STUFFORDER")
#Expose
private Integer sTUFFORDER;
#SerializedName("CUSTOMERID")
#Expose
private String cUSTOMERID;
#SerializedName("EXHIBITS")
#Expose
private List<Object> eXHIBITS = null;
Json response
{
"StuffModels":[
{
"STUFFID":2,
"STUFFCODE":"Gi",
"STUFFNAME":"Giấy",
"STUFFNOTE":"",
"STUFFORDER":2,
"CUSTOMERID":"CAMAU",
"EXHIBITS":[
]
},
ApiUtils Class
public class ApiUtils {
private ApiUtils() {
}
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
public static SOService getSOService() {
return RetrofitClient.getClient(BASE_URL).create(SOService.class);
}
}
Service interface
public interface SOService {
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
RetrofitClient Class
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
My RecyclerView adapter
public class CategogyNameRecyclerViewAdapter extends RecyclerView.Adapter<CategogyNameRecyclerViewAdapter.ViewHolder> {
private List<StuffModel> mItems;
private Context mContext;
private PostItemListener mItemListener;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView titleTv;
PostItemListener mItemListener;
public ViewHolder(View itemView, PostItemListener postItemListener) {
super(itemView);
titleTv = itemView.findViewById(R.id.tvListMenuCategogy);
this.mItemListener = postItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
StuffModel item = getItem(getAdapterPosition());
this.mItemListener.onPostClick(item.getSTUFFID());
notifyDataSetChanged();
}
}
public CategogyNameRecyclerViewAdapter(Context context, List<StuffModel> posts, PostItemListener itemListener) {
mItems = posts;
mContext = context;
mItemListener = itemListener;
}
#Override
public CategogyNameRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View postView = inflater.inflate(R.layout.item_list_text, parent, false);
ViewHolder viewHolder = new ViewHolder(postView, this.mItemListener);
return viewHolder;
}
#Override
public void onBindViewHolder(CategogyNameRecyclerViewAdapter.ViewHolder holder, int position) {
StuffModel item = mItems.get(position);
TextView textView = holder.titleTv;
textView.setText(item.getSTUFFNAME());
}
#Override
public int getItemCount() {
return mItems.size();
}
public void updateAnswers(List<StuffModel> items) {
mItems = items;
notifyDataSetChanged();
}
private StuffModel getItem(int adapterPosition) {
return mItems.get(adapterPosition);
}
public interface PostItemListener {
void onPostClick(long id);
}
}
And my main activity
public class Testttt extends AppCompatActivity {
private CategogyNameRecyclerViewAdapter mAdapter;
private RecyclerView mRecyclerView;
private SOService mService;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.test );
mService = ApiUtils.getSOService();
mRecyclerView = (RecyclerView) findViewById(R.id.rcvCategogyNameMenuTest);
mAdapter = new CategogyNameRecyclerViewAdapter(this, new ArrayList<StuffModel>(0), new CategogyNameRecyclerViewAdapter.PostItemListener() {
#Override
public void onPostClick(long id) {
Toast.makeText(Testttt.this, "Post id is" + id, Toast.LENGTH_SHORT).show();
}
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
mRecyclerView.addItemDecoration(itemDecoration);
loadAnswers();
}
public void loadAnswers() {
mService.getAnswers().enqueue(new Callback<SOAnswersResponse>() {
#Override
public void onResponse(Call<SOAnswersResponse> call, Response<SOAnswersResponse> response) {
Toast.makeText(Testttt.this, "333333333333333333"+response.body(), Toast.LENGTH_SHORT).show();
if(response.isSuccessful()) {
mAdapter.updateAnswers(response.body().getStuffModels());
Log.d("AnswersPresenter", "posts loaded from API");
}else {
int statusCode = response.code();
}
}
#Override
public void onFailure(Call<SOAnswersResponse> call, Throwable t) {
showErrorMessage();
Log.d("AnswersPresenter", "error loading from API");
}
});
}
public void showErrorMessage() {
Toast.makeText(this, "Error loading posts", Toast.LENGTH_SHORT).show();
}
}
The first thing that came in my mind:
Your
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
has a "/" at the the end and your
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
starts with a "/". So there is a double backslash in the url that might leads to the 404 code. Does this solve the problem?
When i call your URL i receive XML. Maybe the API is not configured correctly?
Change your Service interface
public interface SOService {
#GET("stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
it occurred because you have use start with backslash it already added in your base url
I can normally get and show data on Toast message, but I cannot add them to RecyclerView. How can I do this? Please help me. Thanks. I am sharing my code below.
NewsCatalog.java
public class NewsCatalog {
public List<MainNodes> nodes;
}
MainNodes.java
public class MainNodes {
public SubNodes node;
}
SubNodes.java
public class SubNodes {
public String body;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
NewsInterface
public interface NewsService {
String BASE_URL = "http://test.muhabirce.de/app/term/";
#GET("saglik")
Call<NewsCatalog> newsCatalog();
}
Adapter.java
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private ArrayList<SubNodes> subNodes;
private Context context;
public NewsAdapter(ArrayList<SubNodes> subNodes, Context context) {
this.subNodes = subNodes;
this.context = context;
}
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.card_view_items, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(NewsAdapter.ViewHolder holder, int position) {
holder.tvText.setText(subNodes.get(position).getBody());
}
#Override
public int getItemCount() {
return subNodes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvText;
public ViewHolder(View itemView) {
super(itemView);
tvText = (TextView) itemView.findViewById(R.id.tvText);
}
}
}
MainActivity.java
private void bindDatas() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(NewsService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
NewsService newsService = retrofit.create(NewsService.class);
Call<NewsCatalog> requestCatalog = newsService.newsCatalog();
requestCatalog.enqueue(new Callback<NewsCatalog>() {
#Override
public void onResponse(Call<NewsCatalog> call, Response<NewsCatalog> response) {
SubNodes subNodess = new SubNodes();
if (!response.isSuccessful()) {
Log.i("TAG", "Error: " + response.code());
} else {
NewsCatalog catalog = response.body();
try {
for (MainNodes m : catalog.nodes
) {
subNodess.setBody(m.node.body);
}
} catch (Exception e) {
Log.i(TAG, "" + e.getMessage());
}
}
subNodes.add(subNodess);
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<NewsCatalog> call, Throwable t) {
Log.e(TAG, "Error: " + t.getMessage());
}
});
}
}
You should not create a new Subnodes object in response callback. Adapter does not have reference to this object and so the changes in data is not reflected. Update the subnodes object in the recyclerview adapter. Then only notifyDataSetChanged() will reflect the change in data.