gpt4 book ai didi

java - Android 正确的方法是 : where JSON response should be parsed - in UI thread, 还是另一个?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:59 25 4
gpt4 key购买 nike

我只是想知道 - 从网络服务器收到的 JSONObjectJSONArray 应该在 Android 应用程序中解析 - 在主 UI 中或者应该传递到另一个一 ?

例如,我正在使用 Volley 库:

private void fetchResults(){

RequestQueue queue = Volley.newRequestQueue(mContext);
String url = AuthenticationRequester.URL_GET_ALL_ORDERS;
JsonArrayRequest jsonDepartureObj = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
iVolleyCallback.onJSONArraySuccess(jsonArray);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hide the progress dialog
}
});
queue.add(jsonDepartureObj);
}

那么我应该将 iVolleyCallback.onJSONArraySuccess(jsonArray); 放在另一个线程中执行还是可以在主 UI 线程中维护?

让我们假设传入的 JSON 很大并且需要一些时间来处理?

同样的问题与 AsyncTask 和其他在 Android 中使用网络服务的可能方式有关。

最佳答案

最好是,每个需要长时间的任务都应该在另一个线程中处理,以避免重载MainThread:

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

所以如果你知道你有大数据并且需要时间,你会使用新线程,但如果数据很小 而且花费的时间更少,为什么要冒险?也将其移至新线程

关于java - Android 正确的方法是 : where JSON response should be parsed - in UI thread, 还是另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644731/

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