如何处理大量请求

时间:2019-01-23 00:43:55

标签: android json request retrofit2

我使用Retrofit2检索3个数据:id,日期,链接(链接包含另一个json文件)。 <-JSON文件具有3000个条目。 每次获取数据:ID,DATE,LINK时,我都会发送另一个请求以从链接获取数据。 问题是,我总共有450 000个链接,所以我的应用程序每次都崩溃。

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_DATA.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) 
            .build();

    API_DATA api = retrofit.create(API_DATA.class);
    Call<List<DATA>> call = api.getDATA();
    call.enqueue(new Callback<List<DATA>>() {
        @Override
        public void onResponse(Call<List<DATA>> call, Response<List<DATA>> response) {
            List<DATA> dataList = response.body();
            String[] data= new String[dataList.size()];
            for (int i = 0; i < 3000; i++) {
                data[i] = dataList.get(i).getId();
                Log.d(TAG, 
                        "id: " + dataList.get(i).getId() + "\n" +
                        "date: " + dataList.get(i).getDate() + "\n" +
                        "links: " + dataList.get(i).getLinks() + "\n");
            }

        }

        @Override
        public void onFailure(Call<List<DATA>> call, Throwable t) {
            //Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

我想阅读每个条目并在每次获得新链接时发送请求。

这是我的StackStrace:

E: ashmem_create_region failed for 'indirect ref table': Too many open files
E: ashmem_create_region failed for 'indirect ref table': Too many open files
W: Throwing OutOfMemoryError "Could not allocate JNI Env"
W: Throwing OutOfMemoryError "Could not allocate JNI Env"
D: Shutting down VM


    --------- beginning of crash
E: FATAL EXCEPTION: main
    Process: com.quebecdevs.nhl, PID: 19784
    java.lang.OutOfMemoryError: Could not allocate JNI Env
        at java.lang.Thread.nativeCreate(Native Method)
        at java.lang.Thread.start(Thread.java:731)
        at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1359)
        at okhttp3.RealCall$AsyncCall.executeOn(RealCall.java:182)
        at okhttp3.Dispatcher.promoteAndExecute(Dispatcher.java:186)
        at okhttp3.Dispatcher.enqueue(Dispatcher.java:137)
        at okhttp3.RealCall.enqueue(RealCall.java:126)
        at retrofit2.OkHttpCall.enqueue(OkHttpCall.java:117)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.enqueue(ExecutorCallAdapterFactory.java:63)
        at com.quebecdevs.nhl.PlayerStatistique.PlayerStats.test(PlayerStats.java:85)
        at com.quebecdevs.nhl.PlayerStatistique.PlayerStats.access$000(PlayerStats.java:23)
        at com.quebecdevs.nhl.PlayerStatistique.PlayerStats$1.onResponse(PlayerStats.java:65)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1510)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1400)
E: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.quebecdevs.nhl, PID: 19784
    java.lang.OutOfMemoryError: Could not allocate JNI Env
        at java.lang.Thread.nativeCreate(Native Method)
        at java.lang.Thread.start(Thread.java:731)
        at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1359)
        at okhttp3.ConnectionPool.put(ConnectionPool.java:153)
        at okhttp3.OkHttpClient$1.put(OkHttpClient.java:167)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:266)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)

1 个答案:

答案 0 :(得分:0)

在清单文件中添加以下行:android:largeHeap =“ true”。我认为它将解决您的内存不足错误。