从异步改造调用响应填充列表时出现问题

4urapxun  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(235)

我试图用我的数据库中的数据来填充列表,方法是使用reformation进行调用。调用在一个方法中,该方法将响应Map到本地列表并返回它。问题是,本地列表在填充之前会被返回,因为调用是异步的,并且会返回一个空列表。响应本身拥有所需的所有数据。这个问题怎么解决?
调用方法的代码段

fragments = new LinkedHashMap<Integer, RowFragment>();
int subjectCount = this.getTeachers(jwt).size();

for(int i = 0; i < subjectCount; i++){
      RowFragment fragment = new RowFragment();
      fragments.put(i, fragment);
}

方法

public List<Teacher> getTeachers(String jwt){

        APIService apiService = ApiServiceGenerator.createService(APIService.class, jwt);
        Call<List<Teacher>> call = apiService.getTeachers();
        List<Teacher> tempTeachers = new ArrayList<Teacher>();

        call.enqueue(new Callback<List<Teacher>>(){
            @Override
            public void onResponse(Call<List<Teacher>> call, Response<List<Teacher>> response){
                if(response.isSuccessful()){
                    if(response.body().isEmpty() != true){
                        tempTeachers.addAll(response.body());
                    }
                    else{
                        Toast.makeText(BrowseActivity.this, "You have no teachers assigned", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(BrowseActivity.this, "You are not authorized, please log in again", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Teacher>> call, Throwable err) {
                Log.i("err", err.getMessage());
            }
        });
        return tempTeachers;
    }

回应

200 GET https://bakalaurinis.azurewebsites.net/studentmaterial

Content-Type:application/json; charset=utf-8
Date:Mon, 19 Apr 2021 19:50:06 GMT
Server:Microsoft-IIS/10.0
Set-Cookie:ARRAffinitySameSite=22a7daa836b64a8ce56c907737553d08297ff2e76cd06a1f52c29956b9a85c17;Path=/;HttpOnly;SameSite=None;Secure;Domain=bakalaurinis.azurewebsites.net
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:ASP.NET

[{"name":"teacher","surname":"teacher","subject":"teacher","userId":25,"schoolId":5},{"name":"teacher2","surname":"teacher2","subject":"Chemistry","userId":28,"schoolId":5},{"name":"teacher3","surname":"teacher3","subject":"Physics","userId":29,"schoolId":5},{"name":"teacher4","surname":"teacher4","subject":"Math","userId":30,"schoolId":5}]

教师班

public class Teacher {
    public String name;
    public String surname;
    public String subject;
    public int userId;
    public int schoolId;

    public Teacher(String name, String surname, String subject, int userId, int schoolId){
        this.name = name;
        this.surname = surname;
        this.subject = subject;
        this.userId = userId;
        this.schoolId = schoolId;
    }
}

调用可以是同步的,但是如果我理解正确的话,android会抛出一个异常吗?

nhjlsmyf

nhjlsmyf1#

如果你这样做,如果连接很弱怎么办?该值将为null/空。如果你想达到这样的效果,也许你需要使用coroutine等等,所以在这种情况下,你必须在方法中列出它们/获取所有这些值 onResponse . 值将不为空。更改类型方法 getTeachersvoid ```
@Override
public void onResponse(Call<List> call, Response<List> response){
if(response.isSuccessful()){
if(response.body().isEmpty() != true){
tempTeachers.addAll(response.body());
//Do it here
fragments = new LinkedHashMap<Integer, RowFragment>();
final int subjectCount = tempTeachers.size();

        for(int i = 0; i < subjectCount; i++){
            RowFragment fragment = new RowFragment();
            fragments.put(i, fragment);
        }
     }
     else{
        Toast.makeText(BrowseActivity.this, "You have no teachers assigned", Toast.LENGTH_SHORT).show();
     }
}
 else{
    Toast.makeText(BrowseActivity.this, "You are not authorized, please log in again", Toast.LENGTH_SHORT).show();
 }

}

相关问题