#android #api
Вопрос:
Каждый раз, когда я запускаю свое приложение, оно показывает сбой. Класс POJO находится сразу после основной активности
MainActivity.java
package com.example.final_api;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Color;
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 java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
listingdata();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void listingdata()
{
ApiInterface apiInterface = Retrofit.getRetrofit().create(ApiInterface.class);
Call<POJO> listingdata = apiInterface.getdata();
listingdata.enqueue(new Callback<POJO>() {
@Override
public void onResponse(Call<POJO> call, Response<POJO> response) {
if(response.isSuccessful())
{
recyclerAdapter adapter = new recyclerAdapter(response.body().getData());
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<POJO> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
}
});
}
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.myViewHolder>
{
List<POJO.Datum> list;
public recyclerAdapter(List<POJO.Datum> list) {
this.list = list;
}
@NonNull
@Override
public recyclerAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design , parent , false);
recyclerAdapter.myViewHolder viewHolder = new recyclerAdapter.myViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull recyclerAdapter.myViewHolder holder, int position) {
holder.id.setText(String.valueOf(list.get(position).getId()));
holder.name.setText(String.valueOf(list.get(position).getEmployeeName()));
Log.i("Error-name", String.valueOf(list.get(position).getEmployeeName()));
holder.salary.setText(String.valueOf(list.get(position).getEmployeeSalary()));
if(list.get(position).getEmployeeSalary() <= 500)
{
holder.cardView.setCardBackgroundColor(Color.rgb(252,0,0));
}
Log.i("Error-salary", String.valueOf(list.get(position).getEmployeeSalary()));
holder.age.setText(String.valueOf(list.get(position).getEmployeeAge()));
Log.i("Error-age", String.valueOf(list.get(position).getEmployeeAge()));
}
@Override
public int getItemCount() {
return list.size();
}
public class myViewHolder extends RecyclerView.ViewHolder
{
TextView id,name,age,salary;
CardView cardView;
public myViewHolder(@NonNull View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.user_id);
cardView = (CardView) itemView.findViewById(R.id.cardView);
name = (TextView) itemView.findViewById(R.id.name);
age = (TextView) itemView.findViewById(R.id.age);
salary = (TextView) itemView.findViewById(R.id.salary);
}
}
}
}
**POJO.java**
package com.example.final_api;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class POJO {
@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("message")
@Expose
private String message;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public class Datum {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("employee_name")
@Expose
private String employeeName;
@SerializedName("employee_salary")
@Expose
private Integer employeeSalary;
@SerializedName("employee_age")
@Expose
private Integer employeeAge;
@SerializedName("profile_image")
@Expose
private String profileImage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public Integer getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(Integer employeeSalary) {
this.employeeSalary = employeeSalary;
}
public Integer getEmployeeAge() {
return employeeAge;
}
public void setEmployeeAge(Integer employeeAge) {
this.employeeAge = employeeAge;
}
public String getProfileImage() {
return profileImage;
}
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
}
}
Retrofit.java
package com.example.final_api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.converter.gson.GsonConverterFactory;
public class Retrofit {
private static String base_url = "http://dummy.restapiexample.com/api/v1/";
static Gson gson = new GsonBuilder()
.setLenient()
.create();
private static retrofit2.Retrofit retrofit;
public static retrofit2.Retrofit getRetrofit()
{
if(retrofit==null)
{
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(base_url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
APIInterface.java(Интерфейс)
package com.example.final_api;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiInterface {
@GET("employees")
Call<POJO> getdata();
}
I don’t understand wether it is problem with API or my code is not working.
I used same code for other API (https://reqres.in/api/users), though it was returning null but atleast it was getting response.