【问题标题】:How to run many HTTP connections in android?如何在 android 中运行多个 HTTP 连接?
【发布时间】:2017-05-04 12:28:10
【问题描述】:

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

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

如何正确地建立一个接一个的多个连接?

【问题讨论】:

  • 这与“多连接”无关。您正在尝试分配大约 120MB 的堆空间块,这几乎总是会失败。
  • 您在请求中获得了多少条记录?
  • to get some data 什么样的数据?你想把它放在哪里?

标签: java android android-asynctask httpurlconnection


【解决方案1】:

你可以尝试在你的 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 等网络库。

【讨论】:

    【解决方案2】:

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

    或使用VolleyOkHttp

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 2013-01-19
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多