gpt4 book ai didi

android - 使用 Volley 进行异步发布时抛出 OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"

转载 作者:IT老高 更新时间:2023-10-28 23:03:22 40 4
gpt4 key购买 nike

我正在使用 Volley 将存储在本地数据库中的一些数据发布到服务器。问题是当我有大量条目(例如 500)时,我会收到此错误:

 java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
at java.lang.Thread.nativeCreate(Native Method)
at com.android.volley.RequestQueue.start(RequestQueue.java:141)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)
at mypackageName.SomeClass.upload(SomeClass)
at mypackageName.MyClass$MyThred.run(SomeClass.java:387)

这就是我从光标中检索数据并发布帖子的方式

    private class UploadDataThred extends Thread {

@Override
public void run() {
SomeSQLiteHelper someSQLiteHelper = new SomeSQLiteHelper(context);
someSQLiteHelper.getWritableDatabase();
Cursor result = someSQLiteHelper.getAllEvents();
DataUploader dataUploader = new DataUploader(context);

while (result.moveToNext()) {
Build.logError("We're looping!");
while (!waitingForResponse) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
dataUploader.upload(result.getString(0), false, result.getString(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5), result.getString(6), result.getString(7), result.getString(8));

}
someSQLiteHelper.close();
}
}

这是我的 POST 方法

 public void upload(Some parameters go here) {

waitingForResponse = true;
VolleyLog.DEBUG = false;


final JSONObject o = new JSONObject();
o.put(Some data)

RequestQueue requestQueue = Volley.newRequestQueue(context);

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, HOST, o, new Response.Listener<JSONObject>() {


@Override
public void onResponse(JSONObject response) {

if (response.toString().contains("OK")) {

if (columnID != null) {

deleteSingleEntry(columnID);

}


waitingForResponse = false;


}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

waitingForResponse = false;

}
}) {

/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}


};



requestQueue.add(jsonObjReq);

}

如果我在本地数据库中有 400 个条目,然后它会抛出该 execption,那就没问题了。提前谢谢你。

最佳答案

问题是我为每个请求创建了一个新的 RequestQueue。这就是我怀疑它抛出 OutOfMemory 异常的原因。解决方法很简单:

而不是 RequestQueue requestQueue = Volley.newRequestQueue(context);

在方法外声明RequestQueue,仅当前一个为null时才添加新的RequestQueue。

private RequestQueue requestQueue;

public void uploadData(String s) {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context);
Build.logError("Setting a new request queue");
} more request stuff....
}

关于android - 使用 Volley 进行异步发布时抛出 OutOfMemoryError "pthread_create (1040KB stack) failed: Try again",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35701501/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com