如何在 android 中 运行 多个 HTTP 连接?

How to run many HTTP connections in android?

我有一个包含 id 的列表,我将在 URL 中使用它,以使用 HttpConnection 获取一些数据。我试图在循环中使用 AsyncTask,但这是个坏主意,因为我的程序崩溃了:

java.lang.OutOfMemoryError: Failed to allocate a 124606360 byte allocation with 16777216 free bytes and 103MB until OOM

如何正确连接多个连接?

尝试方法 new AsyncTask().executeOnExecutor()

或使用Volley or OkHttp

您可以尝试在您的 AndroidManifest.xml

中添加 android:largeHeap="true"
<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

或者您可以尝试使用我们的 Volley、Okhttp、Retrofit 等网络库