简体   繁体   中英

Android Retrofit 2.1.0 Response.body() is null, status code is 404

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM