0

I am retrieveing a list of data from server to be displayed into recyclerview in android using retrofit.
The retrieval is working fine, the data called is displayed and no problem.

But the problem is, I want to check if the data I get is empty or not, because I want to do some action from this checking.

I've tried this code but it keeps checking only successful do the request, not the JSON data content that it brought from server.

getData.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {

if (response.body() != null){
Toast.makeText(getApplicationContext(), "Data is not empty", Toast.LENGTH_LONG).show();
} else {
// want to do some action here after the checking
Toast.makeText(getApplicationContext(), "Data isempty", Toast.LENGTH_LONG).show();
}
someList = new ArrayList<ModelItem>();
someList= response.body().getItem();
adapter= new Adapter(getApplicationContext(), someList);

recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(getApplicationContext(), getString(R.string.error_connection), Toast.LENGTH_LONG).show();
}
});

I want to check the inner response if it empty or not, thanks

Muhammad Faisal
  • 734
  • 10
  • 24
  • https://stackoverflow.com/questions/45434716/retrofit2-how-to-properly-check-the-validity-of-response-body – John Joe May 14 '19 at 03:14

4 Answers4

1

use below code, before you map Json to POJO.

String response = basicResponse.getResponse();
if(response!=null){
   String responseBody = response.body();
   if(!responseBody.isEmpty()){
      // non empty response, you can map Json via Gson to POJO classes...

   }
   else{
       // empty response...
   }
}
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
1

First check validation of response then it's body and then do rest of the work under there:

@Override
    public void onResponse(Call<Model> call, Response<Model> response) {

    if ( response.body() != null){
    someList = new ArrayList<ModelItem>();
    someList= response.body().getItem();
    adapter= new Adapter(getApplicationContext(), someList);

    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    } else {
    // want to do some action here after the checking
    Toast.makeText(getApplicationContext(), "Data isempty", Toast.LENGTH_LONG).show();
    }

    }

    @Override
    public void onFailure(Call<Model> call, Throwable t) {
    Toast.makeText(getApplicationContext(), getString(R.string.error_connection), Toast.LENGTH_LONG).show();
    }
    });
Sultan Mahmud
  • 1,245
  • 8
  • 18
  • The `if (response != null && response.body() != null)` makes android lint shows that it is the same as `if (response.body() != null)` sir, it return the same.. – Muhammad Faisal May 14 '19 at 03:55
0

This May Help you.

@Override
public void onResponse(Call<String> call, Response<String> response) {

  String responseBody = response.body();

   if(!responseBody.isEmpty()){
      // Create here  Json via Gson to POJO classes...
               try {
                        Gson gson = new Gson();
                        Type type = new TypeToken<List<Model>>() {
                        }
                                .getType();
                        objModel= gson.fromJson(responseBody, type);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }

   }
   else{
       // empty response...
   }

    }
    @Override
    public void onFailure(Call<Model> call, Throwable t) {
    Toast.makeText(getApplicationContext(), getString(R.string.error_connection), Toast.LENGTH_LONG).show();
    }
    });
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
0

You won't be able to assign response.body() to a String - you will need to call the string method on it like:

String responseBody = response.body().string();
Aki
  • 2,818
  • 1
  • 15
  • 23
ADaly
  • 21
  • 1
  • 6