Retrofit 在使用 LiveData 时提供 onFailure 回调 - 无法在没有参数的情况下调用 public androidx.lifecycle.LiveData()

Retrofit gives onFailure Callback When Using LiveData - Failed to invoke public androidx.lifecycle.LiveData() with no args

I/System.out: Failure -> Failed to invoke public androidx.lifecycle.LiveData() with no args

我正在从 API 获取数据。我正在尝试使用 MVVM,我是 MVVM 的新手。所以我从 ViewModel 调用 api 并且我 return 方法中的数据。我正在从我的 MainActivity 中观察该方法。但我不知道为什么会给我以下错误。

Retrofit 进入失败回调。但是当我没有使用 LiveData 时它正在成功回调中。

I/System.out: Failure -> Failed to invoke public androidx.lifecycle.LiveData() with no args

我的视图模型

public class MainViewModel extends ViewModel {

  private ApiInterface apiInterface;
  private LiveData < List < Feature >> featuresList;

  public MainViewModel() {
    apiInterface = ApiClient.getClient().create(ApiInterface.class);
    getEqData();
    featuresList = new MutableLiveData < > ();
  }

  public void getEqData() {

    apiInterface.getTodos().enqueue(new Callback < EarthquakeData > () {
      @Override
      public void onResponse(Call < EarthquakeData > call, Response < EarthquakeData > response) {
        featuresList = response.body().features;
      }

      @Override
      public void onFailure(Call < EarthquakeData > call, Throwable t) {
        System.out.println("Failure -> " + t.getMessage());
      }
    });
  }

  public LiveData < List < Feature >> returnEqData() {
    return featuresList;
  }

}

主要活动

public class MainActivity extends AppCompatActivity {

  private static final String TAG = "sagar";
  private MainViewModel mainViewModel;
  private RecyclerView rvEarthquake;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rvEarthquake = findViewById(R.id.rvEarthquake);
    EarthquakeAdapter adapter = new EarthquakeAdapter(this);
    rvEarthquake.setLayoutManager(new LinearLayoutManager(this));
    rvEarthquake.setAdapter(adapter);

    mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    mainViewModel.returnEqData().observe(this, new Observer < List < Feature >> () {
      @Override
      public void onChanged(List < Feature > features) {
        adapter.setData(features);
      }
    });
  }

}

使用 setValue 或 postValue 在 featuresList 中设置值,如下所示

featuresList.setValue(response.body().features);