一种方法从 Web 服务器获得响应,另一种方法需要该方法对 return 的回答,不幸的是我得到了 null
One method get respond from WebServer and other method need answer of that method to return something , and unfortunately I got null
在 MainActivityViewModel
class 我有一个 Getter 方法,它 return 是 CurrentWeather
(pojo class) 的一个实例,这个方法需要 OnResponse
方法的响应,但我第一次得到 null。
从 MainActivity
调用的第一个方法,viewModel
不是 null 但 currentWeather
实例是。
MainActivityViewModel viewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
currentWeather = viewModel.getCurrentWeather();
不知道能不能在第一种方法returncurrentWeather
之前要求稍等片刻
public class MainActivityViewModel extends ViewModel implements Callback<ResponseBody> {
private CurrentWeather currentWeather;
public CurrentWeather getCurrentWeather() {
if (currentWeather == null) {
createCurrentWeather("London");
}
return currentWeather;
}
public void createCurrentWeather(String city) {
RetrofitApiManager.getInstance().getCurrentWeatherApi(this, city);
}
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
ResponseBody body = response.body();
try {
String serverResponde = body.string();
Timber.e(serverResponde);
Gson gson = new Gson();
currentWeather = gson.fromJson(serverResponde, CurrentWeather.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
}
这是因为返回响应需要一段时间。
通常,您需要一个 LiveData
对象来从后台任务中获取结果。
在您的 MainActivityViewModel
中,添加以下内容:
private MutableLiveData currentWeatherData = new MutableLiveData<CurrentWeather>();
public LiveData<CurrentWeather> getCurrentWeatherData() {
return currentWeatherData;
}
收到回复后,更新您的 LiveData
currentWeather = gson.fromJson(serverResponde, CurrentWeather.class);
currentWeatherData.postValue(currentWeather);
在你的activity中,你需要观察这个LiveData。
viewModel.getCurrentWeatherData().observe(this, new Observer<CurrentWeather>() {
@Override
public void onChanged(CurrentWeather c) {
// Do whatever you want with c.
}
});
在 MainActivityViewModel
class 我有一个 Getter 方法,它 return 是 CurrentWeather
(pojo class) 的一个实例,这个方法需要 OnResponse
方法的响应,但我第一次得到 null。
从 MainActivity
调用的第一个方法,viewModel
不是 null 但 currentWeather
实例是。
MainActivityViewModel viewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
currentWeather = viewModel.getCurrentWeather();
不知道能不能在第一种方法returncurrentWeather
之前要求稍等片刻
public class MainActivityViewModel extends ViewModel implements Callback<ResponseBody> {
private CurrentWeather currentWeather;
public CurrentWeather getCurrentWeather() {
if (currentWeather == null) {
createCurrentWeather("London");
}
return currentWeather;
}
public void createCurrentWeather(String city) {
RetrofitApiManager.getInstance().getCurrentWeatherApi(this, city);
}
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
ResponseBody body = response.body();
try {
String serverResponde = body.string();
Timber.e(serverResponde);
Gson gson = new Gson();
currentWeather = gson.fromJson(serverResponde, CurrentWeather.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
}
这是因为返回响应需要一段时间。
通常,您需要一个 LiveData
对象来从后台任务中获取结果。
在您的 MainActivityViewModel
中,添加以下内容:
private MutableLiveData currentWeatherData = new MutableLiveData<CurrentWeather>();
public LiveData<CurrentWeather> getCurrentWeatherData() {
return currentWeatherData;
}
收到回复后,更新您的 LiveData
currentWeather = gson.fromJson(serverResponde, CurrentWeather.class);
currentWeatherData.postValue(currentWeather);
在你的activity中,你需要观察这个LiveData。
viewModel.getCurrentWeatherData().observe(this, new Observer<CurrentWeather>() {
@Override
public void onChanged(CurrentWeather c) {
// Do whatever you want with c.
}
});