2017-01-17 3 views
0

ApiClient.getClient()。create(ApiInterface.class)は、Retrofitクライアントを常に再作成するよう呼び出されています。 (同じことがrecyclerViewにも当てはまります)。私はシングルトンを使うべきだと思っています.Android Studioでどのように見つけられたのでしょう?ここに私のコードは次のとおりです。Androidオブジェクトの作成と初期化に関するいくつかの問題

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

に作成されているどのように多くのオブジェクトを参照するには、次の

  1. 電話またはエミュレータ上でデバッグモードでアプリケーションを実行します。
  2. Androidモニタータブに移動します。
  3. [モニター]タブをクリックします。
  4. [ヒープダンプ]アイコンをクリックします。

この後、新しいタブがすべてのメモリ使用情報と共にエディタに表示されます。

https://developer.android.com/studio/profile/am-memory.html

幸運! :-)

関連する問題