Android 如何在其他地方使用改装响应
我正在我的应用程序中使用改型,并从服务器接收数据。 在改造中,我从responseBody中的服务器接收数据,但当我想使用这些接收到的数据时,我的阵列为空 这是我的班级:Android 如何在其他地方使用改装响应,android,retrofit,Android,Retrofit,我正在我的应用程序中使用改型,并从服务器接收数据。 在改造中,我从responseBody中的服务器接收数据,但当我想使用这些接收到的数据时,我的阵列为空 这是我的班级: public class ActivityApplicationsList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class ActivityApplicationsList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_applications_list);
Log.i(TAG , "size of response array: " + populateDataFromServer().size())
//this size is 0 but have to be 4 !
}
private ArrayList<StructApplication> populateDataFromServer() {
final ArrayList<StructApplication> mine = new ArrayList<>();
final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
Call<ArrayList<AppModel>> call = service.getApp();
call.enqueue(new Callback<ArrayList<AppModel>>() {
@Override
public void onResponse(Call<ArrayList<AppModel>> call, Response<ArrayList<AppModel>> response) {
ArrayList<AppModel> newAppModel = response.body();
for(int i=0 ; i < newAppModel.size();i++){
StructApplication structApplication = new StructApplication();
structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
structApplication.setId(newAppModel.get(i).getAppId());
structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());
mine.add(structApplication);
}
}
@Override
public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
}
});
return mine;
}
}
公共类活动应用程序列表扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u应用程序\u列表);
Log.i(标记,“响应数组的大小:”+populateDataFromServer().size())
//此大小为0,但必须为4!
}
private ArrayList populateDataFromServer(){
最终ArrayList mine=新ArrayList();
最终APIService服务=ServiceGenerator.createService(APIService.class,“2015-03-01 14:26:00”,“123456”,“123456”);
召唤
正如您所看到的,我正确地接收了所有数据,但是当我在这个类的onCreate中使用这个响应时,它是空的
我真的很感谢你对这个奇怪问题的回答。它先返回,然后再执行,试试这个方法
private ArrayList<StructApplication> populateDataFromServer() {
final ArrayList<StructApplication> mine = new ArrayList<>();
final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
Call<ArrayList<AppModel>> call = service.getApp();
Response<ArrayList<AppModel>> response = responseCall.execute();
ArrayList<AppModel> newAppModel = response.body();
for(int i=0 ; i < newAppModel.size();i++){
StructApplication structApplication = new StructApplication();
structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
structApplication.setId(newAppModel.get(i).getAppId());
structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());
mine.add(structApplication);
}
return mine;
}
private ArrayList populateDataFromServer(){
最终ArrayList mine=新ArrayList();
最终APIService服务=ServiceGenerator.createService(APIService.class,“2015-03-01 14:26:00”,“123456”,“123456”);
Call Call=service.getApp();
Response=responseCall.execute();
ArrayList newAppModel=response.body();
对于(int i=0;i
首先返回,然后执行,请尝试以下方法
private ArrayList<StructApplication> populateDataFromServer() {
final ArrayList<StructApplication> mine = new ArrayList<>();
final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
Call<ArrayList<AppModel>> call = service.getApp();
Response<ArrayList<AppModel>> response = responseCall.execute();
ArrayList<AppModel> newAppModel = response.body();
for(int i=0 ; i < newAppModel.size();i++){
StructApplication structApplication = new StructApplication();
structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
structApplication.setId(newAppModel.get(i).getAppId());
structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());
mine.add(structApplication);
}
return mine;
}
private ArrayList populateDataFromServer(){
最终ArrayList mine=新ArrayList();
最终APIService服务=ServiceGenerator.createService(APIService.class,“2015-03-01 14:26:00”,“123456”,“123456”);
Call Call=service.getApp();
Response=responseCall.execute();
ArrayList newAppModel=response.body();
对于(int i=0;i
这是因为您正在响应到来之前打印列表大小。As请求正在异步发送,并且您正在尝试在onResponse()
回调方法之前获取大小。
尝试添加这一行
Log.i(TAG , "size of response array: " + populateDataFromServer().size())
在onResponse()
方法中,在mine.add(structApplication);
之后,您将看到正确的大小。这是因为您正在响应到来之前打印列表大小。因为请求是异步发送的,并且您正在尝试在onResponse()
回调方法之前获取大小。
尝试添加这一行
Log.i(TAG , "size of response array: " + populateDataFromServer().size())
在onResponse()
方法中,在mine.add(structApplication);
之后,您将看到正确的大小。这是由于api调用导致的,因为api需要几秒钟才能获得响应,同时您返回了mine
数组。因此,一旦从服务器获得值,请返回响应
你喜欢这样吗
为该类创建一个全局实例,如下所示
public class ActivityApplicationsList extends Activity {
ArrayList<StructApplication> mine = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_applications_list);
populateDataFromServer();
Log.i(TAG , "size of response array: " + mine.size());
}
private void populateDataFromServer() {
final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
Call<ArrayList<AppModel>> call = service.getApp();
call.enqueue(new Callback<ArrayList<AppModel>>() {
@Override
public void onResponse(Call<ArrayList<AppModel>> call, Response<ArrayList<AppModel>> response) {
ArrayList<AppModel> newAppModel = response.body();
for(int i=0 ; i < newAppModel.size();i++){
StructApplication structApplication = new StructApplication();
structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
structApplication.setId(newAppModel.get(i).getAppId());
structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());
mine.add(structApplication);
}
}
@Override
public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
}
});
}
}
公共类活动应用程序列表扩展活动{
ArrayList mine=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u应用程序\u列表);
已填充DataFromServer();
Log.i(标记,“响应数组的大小:”+mine.size());
}
private void populateDataFromServer(){
最终APIService服务=ServiceGenerator.createService(APIService.class,“2015-03-01 14:26:00”,“123456”,“123456”);
Call Call=service.getApp();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
ArrayList newAppModel=response.body();
对于(int i=0;i
这是由于api调用造成的,因为api需要几秒钟才能获得响应,同时您返回了您的我的
数组。所以,一旦您从服务器获得值,请返回响应
你喜欢这样吗
为该类创建一个全局实例,如下所示
public class ActivityApplicationsList extends Activity {
ArrayList<StructApplication> mine = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_applications_list);
populateDataFromServer();
Log.i(TAG , "size of response array: " + mine.size());
}
private void populateDataFromServer() {
final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
Call<ArrayList<AppModel>> call = service.getApp();
call.enqueue(new Callback<ArrayList<AppModel>>() {
@Override
public void onResponse(Call<ArrayList<AppModel>> call, Response<ArrayList<AppModel>> response) {
ArrayList<AppModel> newAppModel = response.body();
for(int i=0 ; i < newAppModel.size();i++){
StructApplication structApplication = new StructApplication();
structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
structApplication.setId(newAppModel.get(i).getAppId());
structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());
mine.add(structApplication);
}
}
@Override
public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
}
});
}
}
公共类活动应用程序列表扩展活动{
ArrayList mine=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u应用程序\u列表);
已填充DataFromServer();
Log.i(标记,“响应数组的大小:”+mine.size());
}
私有无效填充数据