我正在使用改造来获取我的回收 View 中的 json 数据。它几周前工作正常,但我现在运行代码,然后它只加载一次数据,然后在我更改任何文本或在 json 数据中添加新值时多次加载,它总是在加载时加载相同的初始数据。我没有使用任何缓存属性,奇怪的是,一旦它第一次加载,那么如果我删除我的 json,那么它仍然会加载数据,而不是抛出异常并给出找不到 json 的错误。
我错过了什么。我改变了retrofit的版本,但它似乎不起作用。这是我的 Mainactivity 代码是
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<List<RetroPhoto>> call = service.getAllPhotos();
call.enqueue(new Callback<List<RetroPhoto>>() {
@Override
public void onResponse(Call<List<RetroPhoto>> call, Response<List<RetroPhoto>> response) {
progressDoalog.dismiss();
generateDataList(response.body());
}
@Override
public void onFailure(Call<List<RetroPhoto>> call, Throwable t) {
progressDoalog.dismiss();
Toast.makeText(NewsActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
/*Method to generate List of data using RecyclerView with custom adapter*/
private void generateDataList(List<RetroPhoto> photoList) {
recyclerView = findViewById(R.id.customRecyclerView);
adapter = new CustomAdapter(this,photoList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NewsActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
GetDataService的代码是
public interface GetDataService {
@GET("b.json")
Call<List<RetroPhoto>> getAllPhotos();
}
我的 gradle 是
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
预先感谢您的帮助。我花了 3 个多小时把头撞在地板上,试图弄清楚这几周我什至没有碰过代码的情况下到底发生了什么。
最佳答案
我意识到问题出在服务器端,因为我正在使用 ipage 主机,我将 json 移动到其他主机,并立即根据请求在应用程序中显示更改,但 ipage 没有。所以代码没问题。
关于java - 强制改造以加载新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60931705/