正在调用ApiClient.getClient()。create(ApiInterface.class)来重建Retrofit客户端。这个成本应该只发生一次。(同样适用于recyclerView)。我认为应该使用singleton ..我也可以在Android Studio中找到多少对象创建...任何帮助。这是我的代码:
public void fetchData() {
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<List<Currency>> call = apiService.getData("");
if (isNetworkAvailable()) {
call.enqueue(new Callback<List<Currency>>() {
@Override
public void onResponse(Call<List<Currency>> call, Response<List<Currency>> response) {
if (response.isSuccessful()) {
List<Currency> rew = response.body();
if(( moduloLoop % 2) == 0 ) {
for (int i =0; i < rew.size(); i++){
mBuy.add(i, String.valueOf(rew.get(i).getBuy())+","+i);
mSell.add(i, String.valueOf(rew.get(i).getSell())+","+i);
mSharePreference.saveArray(MainActivity.this, mBuy,"BuyValue");
mSharePreference.saveArray(MainActivity.this, mSell,"SellValue");
}
mSell.clear();
mBuy.clear();
}
if( !isTablet){
recyclerView.setAdapter(new CurrencyAdapter(response.body(), R.layout.list_item_trade, MainActivity.this));
}else {
recyclerView.setAdapter(new CurrencyAdapter(response.body(), R.layout.tablet_list_item_trade, MainActivity.this));
}
} else {
alertError(MainActivity.this.getString(R.string.DownloadFailed));
}
}
@Override
public void onFailure(Call<List<Currency>> call, Throwable t) {
alertError(t.getMessage()+"");
}
});
}else {
alertError(this.getString(R.string.network_unavailable));
}
}
// Trying to create a polling mechanism to update rates every couple of //seconds.
Handler h = new Handler();
private static final int DELAY = 25000;
Runnable runnable;
@Override
protected void onStart() {
h.postDelayed(new Runnable() {
public void run() {
fetchData();
runnable = this;
moduloLoop++;
h.postDelayed(runnable, DELAY);
}
}, DELAY);
super.onStart();
}
public class ApiClient {
private static final String BASE_URL = "http://massignment.xxxxx.com/api/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
public interface ApiInterface {
@GET("rates")
Call<List<Currency>> getData(@Query("") String values);
}
答案 0 :(得分:0)
要查看创建了多少个对象:
在此之后,编辑器中将出现一个新选项卡,其中包含所有内存使用信息。
祝你好运! : - )