retrofit2を使用してAPIからデータを取得しようとしています。これは私が改造サービス呼び出しています私のヘルパークラスである入れ子になっているレトロフィットサービスを呼び出して、データを正しく取得するにはどうすればよいですか? [retrofit2]
public class Corporations{
Company company = new Company();
ArrayList<Departments> departments = new ArrayList<>();
ArrayList<OfficeLocations> officeLocations = new ArrayList<>();
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public ArrayList<Departments> getDepartments() {
return departments;
}
public void setDepartments(ArrayList<Departments> departments) {
this.departments = departments;
}
public ArrayList<OfficeLocations> getofficeLocations() {
return officeLocations;
}
public void setofficeLocations(ArrayList<OfficeLocations> officeLocations) {
this.officeLocations = officeLocations;
}
public class Company {
Integer CompanyId;
String CompanyName;
public Company() {
}
public Integer getCompanyId() {
return CompanyId;
}
public void setCompanyId(Integer companyId) {
this.CompanyId = companyId;
}
public String CompanyName() {
return CompanyName;
}
public void setCompanyName(String CompanyName) {
this.CompanyName = CompanyName;
}
}
public class Departments {
Integer CompanyId, DepartmentId;
String DepartmentName;
public Departments() {
}
public Integer getCompanyId() {
return CompanyId;
}
public void setCompanyId(Integer CompanyId) {
CompanyId = CompanyId;
}
public Integer getDepartmentId() {
return DepartmentId;
}
public void setDepartmentId(Integer DepartmentId) {
this.DepartmentId = DepartmentId;
}
public String getDepartmentName() {
return DepartmentName;
}
public void setDepartmentName(String DepartmentName) {
this.DepartmentName = DepartmentName;
}
}
public class OfficeLocations {
Integer LocationId, CompanyId;
String LocationName;
public OfficeLocations() {
}
public Integer getLocationId() {
return LocationId;
}
public void setLocationId(Integer LocationId) {
this.LocationId = LocationId;
}
public String getLocationName() {
return fundId;
}
public void setLocationName(String LocationName) {
this.LocationName = LocationName;
}
public Integer getCompanyId() {
return CompanyId;
}
public void setCompanyId(Integer CompanyId) {
CompanyId = CompanyId;
}
}
}
Corporations.class:
私のデータのモデルがこれです
public class Helper {
public interface CorporationsHelperCallbacks() {
void setCompanies(ArrayList<Corporations.Company> companyList);
}
public interface DepartmentHelperCallbacks(){
void setDepartments(ArrayList<Corporations.Departments> deptList);
void setLocations(ArrayList<Corporations.OfficeLocations> locationList);
}
public void getCompanies(String apiKey, @Nullable final CorporationsHelperCallbacks callback){
Call<ArrayList<Corporations.Company>> call = apiInterface.getSolutionProducts(apiKey);
call.enqueue(new Callback<ArrayList<MySolutionsProductModel>>() {
@Override
public void onResponse(Call<ArrayList<Corporations.Company>> call, Response<ArrayList<Corporations.Company>> response) {
try {
ArrayList list = response.body();
callback.setCompanies(list);
} catch (Exception ex){
Toast.makeText(context,ex.getMessage(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ArrayList<Corporations.Company>> call, Throwable t) {
Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
public void getCompanyData(int companyId, String apiKey, @Nullable final CorporationsHelperCallbacks callback){
Call<ArrayList<Corporations>> call = apiInterface.getSolutionProducts(comapanyId, apiKey);
call.enqueue(new Callback<ArrayList<Corporations>>() {
@Override
public void onResponse(Call<ArrayList<Corporations>> call, Response<ArrayList<Corporations>> response) {
try {
ArrayList list = response.body();
callback.setDepartments(list.getDepartments());
callback.setLocations(list.getLocations());
} catch (Exception ex){
Toast.makeText(context,ex.getMessage(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ArrayList<Corporations>> call, Throwable t) {
Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
をそして、これはどのようにあります私は改造方法を呼び出す必要があります:
public void getData{
helper.getCompanies(apiKey, new Helper.CorporationsHelperCallbacks() {
@Override
public void setCompanies(final ArrayList<Corporations.Company> companyList) {
CorporationsModel corporations;
for (int i = 0; i < companyList.size(); i++) {
corporations = new CorporationsModel()
corporations.getcompanyList().setCompanyId(companyList.get(i).getCompanyId());
corporations.getcompanyList().setCompanyName(companyList.get(i).getCompanyName());
helper.getCompanyData(companyList.get(i).getCompanyId(), apiKey, new Helper.DepartmentHelperCallbacks() {
@Override
public void setDepartments(ArrayList<Corporations.Departments> deptList) {
if (deptList.size() > 0) {
for (int j = 0; j < deptList.size(); j++) {
corporations.getDepartments().get(j).setDepartmentId(deptList.get(j).getDepartmentId());
corporations.getDepartments().get(j).setDepartmentName(deptList.get(j).getDepartmentName());
corporations.getDepartments().get(j).setCompanyId(deptList.get(j).getCompanyId());
}
}
}
@Override
public void setLocations(ArrayList<Corporations.OfficeLocations> locationList) {
if (locationList.size() > 0) {
for (int k = 0; k < locationList.size(); k++) {
corporations.getofficeLocations().get(k).setLocationId(locationList.get(k).getLocationId()));
corporations.getofficeLocations().get(k).setLocationName(locationList.get(k).getLocationName()));
corporations.getofficeLocations().get(k).setCompanyId(locationList.get(k).getCompanyId()));
}
}
}
companyList.add(corporations);//this is a global variable
}
String str = gson.toJson(companyList);
sharedPreferenceEditor.putString("companyList",str);
sharedPreferenceEditor.commit();
}
});
}
上記の呼び出しを実行し、SharedPreferenceに会社データのリストを格納する必要があります。しかし、レトロフィットの非同期性により、内部サービスが呼び出されますが、データがリストに追加されなければ、共有プリファレンスが実行されます。データはこの形式で保存する必要があります。これはapiの設計方法であるためです。
どうすれば正しく行うことができますか?どんな助けもありがとうございます。ありがとう
さて、最後の反復に
setLocations()
コールバック、私はそれが動作すると思います。ありがとう – vinitpradhan18それを行う他のエレガントな方法はありますか? – vinitpradhan18
@ vinitpradhan18ありますが、データベースを使用します。そのようにすれば、DBに会社を作成すると、必要なときにフィールド(場所、部署)を更新するだけで済みます – andrei