gpt4 book ai didi

Android Volley 加载速度慢并卡住进度对话框

转载 作者:行者123 更新时间:2023-11-30 02:49:10 24 4
gpt4 key购买 nike

谁能告诉我如何提高 Volley 的速度和删除缓存功能吗?

我使用 onResponse 方法从包含 500 多条记录的网络服务加载数据。但是它显示数据的速度非常慢,并且会卡住进度对话框,直到 Volley 加载所有数据。请帮忙。下面是我的代码-

    C.progressStart(context, "Loading Data...", "Please Wait");
String tag_string_req = "string_req";
final List<List<String>> values = new ArrayList<List<String>>();
datas = new ArrayList<ResultData>();
String eid = id;
url = C.EVENT + eid;
request = new StringRequest(Method.GET, url,
new Listener<String>() {

@Override
public void onResponse(String arg0) {
C.progressStop();
// TODO Auto-generated method stub
JsonParser parent = new JsonParser(arg0);
header.setText(parent.getValue("name"));
String resp = parent.getValue("data");
-----
-----
}, new ErrorListener() {

@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
C.ToastShort(context,
"Please check your network connection");
C.progressStop();
}
});
queue.add(request);

最佳答案

您应该使用 parseNetworkResponse 方法来解析数据,然后将响应传递给 onResponse 方法,如下所示:

parseNetworkResponse(NetworkResponse response) {
// Parse the response here.
// When done parsing the response return it:
if(success) {
return Response.success(theParsedResult, HttpHeaderParser.parseCacheHeaders(null));
// HttpHeaderParser.parseCacheHeaders(null) will not cache the response.
} else {
// Something was wrong with the response, so return an error instead.
return Response.error(new ParseError(new Exception("My custom message about the error)));
}
}

parseNetworkResponse 方法将在工作线程而不是主线程(UI 线程)上运行。 onResponse 方法将在 UI 线程上调用,这就是您在 ProgressDialog 中看到“卡顿”的原因。

编辑:

鉴于您正在使用 StringRequest 类,您不能调用 parseNetworkResponse 方法,除非您对 StringRequest 进行子类化。但是,由于您实际上是从响应中解析 JSON 对象,因此我建议改用 JsonRequest 类,而这个类希望您覆盖 parseNetworkResponse

所以你的请求应该是这样的:

JsonRequest<List<MyParsedCustomObjects>> request =  JsonRequest<List<MyParsedCustomObjects>>(Request.Method.GET, "my_url", null, new Response.Listener<List<MyParsedCustomObjects>>() {
@Override
public void onResponse(List<MyParsedCustomObjects> response) {
// Populate your list with your parsed result here.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Response<List<MyParsedCustomObjects>> parseNetworkResponse(NetworkResponse response) {
// Handle the response accordingly off the main thread. The return the parsed result.
return null; // Return your parsed List of objects here - it will be parsed on to the "onResponse" method.
}
};

一开始有点难理解,如果有什么不明白的地方请告诉我:-)

关于Android Volley 加载速度慢并卡住进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24506208/

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