1
我已經實現了一個viewpager,其中包含兩個片段,每個片段都發送一個GET請求以抽籤。由於這兩個片段是同時加載的,所以在volley中使用默認請求隊列時,會出現單個客戶端連接管理器錯誤。這裏是我的駐留在一個單獨的請求隊列代碼:Android volley - 來自viewpager片段的併發/並行獲取請求
client = new DefaultHttpClient();
mRequestQueue = Volley.newRequestQueue(ctx.getApplicationContext(), new HttpClientStack(client));
這將導致以下錯誤:
W/SingleClientConnManager﹕ Invalid use of SingleClientConnManager:connection still allocated.
Make sure to release the connection before allocating another one
我切換到使用ThreadedConnectionManager,它似乎已經解決了這個問題。以下解決方案:
DefaultHttpClient mDefaultHttpClient = new DefaultHttpClient();
final ClientConnectionManager mClientConnectionManager = mDefaultHttpClient.getConnectionManager();
final HttpParams mHttpParams = mDefaultHttpClient.getParams();
final ThreadSafeClientConnManager mThreadSafeClientConnManager = new ThreadSafeClientConnManager(mHttpParams, mClientConnectionManager.getSchemeRegistry());
mDefaultHttpClient = new DefaultHttpClient(mThreadSafeClientConnManager, mHttpParams);
final HttpStack httpStack = new HttpClientStack(mDefaultHttpClient);
mRequestQueue = Volley.newRequestQueue(ctx.getApplicationContext(), httpStack);
我的問題是:是否有解決的更好的方法這個問題,或者是我的解決方案是否合適?根據我的用例,這裏最好的做法是什麼?
您是否每次都在製作newRequestQueue?或者它只是第一次創建? –
它只創建一次 –
試圖幫助。請看答案。 –