使用 Volley 请求的随机延迟
Random delays using Volley request
我正在使用 Volley singleton
将 POST 消息发送到 java 中创建的 NanoHTTPD
服务器。消息短,处理时间快,同时不超过1条消息。
问题是处理请求的队列中随机存在一些延迟,导致加载时间为 3-4 秒。最常见的情况是第一条消息(这没问题,我想是在创建一些实例)或者当我在任何消息后等待超过 5-10 秒时。但是,如果我发送,例如 100 POST 条消息,中间有 1 秒的延迟,则没有问题。
我检查了服务器,没有生成请求,所以问题出在客户端。
在这些情况下,logcat 显示:
D/Volley: [839] BasicNetwork.logSlowRequests: HTTP response for
request=<[ ] http://192.168.2.8:8765 0x355fcdd NORMAL 1>
[lifetime=4768], [size=115], [rc=200], [retryCount=0]
做一些 google logSlowRequests 意味着......请求需要时间在客户端处理。
另一个尝试的解决方案是增加 threadPoolSize 但这没有用。总是第一个请求要花很多时间。
这是单例代码:
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private DiskBasedCache cache;
private BasicNetwork network;
/**
* @param context
*/
private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
/**
* @param context
* @return
*/
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
/**
* @return
*/
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// Instantiate the cache
cache = new DiskBasedCache(mCtx.getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
network = new BasicNetwork(new HurlStack());
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = new RequestQueue(cache, network, 20);
mRequestQueue.start();
}
return mRequestQueue;
}
/**
* @param req
* @param <T>
*/
public <T> void addToRequestQueue(Request<T> req) {
req.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}
}
进行了更深入的调试,问题出在服务器上:
this.remoteHostname = !inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()?inetAddress.getHostName().toString():"localhost";
这会生成需要几秒钟的 DNS 查找。
库中已经上传了一个问题 github 项目:
我正在使用 Volley singleton
将 POST 消息发送到 java 中创建的 NanoHTTPD
服务器。消息短,处理时间快,同时不超过1条消息。
问题是处理请求的队列中随机存在一些延迟,导致加载时间为 3-4 秒。最常见的情况是第一条消息(这没问题,我想是在创建一些实例)或者当我在任何消息后等待超过 5-10 秒时。但是,如果我发送,例如 100 POST 条消息,中间有 1 秒的延迟,则没有问题。
我检查了服务器,没有生成请求,所以问题出在客户端。
在这些情况下,logcat 显示:
D/Volley: [839] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.2.8:8765 0x355fcdd NORMAL 1> [lifetime=4768], [size=115], [rc=200], [retryCount=0]
做一些 google logSlowRequests 意味着......请求需要时间在客户端处理。
另一个尝试的解决方案是增加 threadPoolSize 但这没有用。总是第一个请求要花很多时间。
这是单例代码:
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private DiskBasedCache cache;
private BasicNetwork network;
/**
* @param context
*/
private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
/**
* @param context
* @return
*/
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
/**
* @return
*/
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// Instantiate the cache
cache = new DiskBasedCache(mCtx.getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
network = new BasicNetwork(new HurlStack());
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = new RequestQueue(cache, network, 20);
mRequestQueue.start();
}
return mRequestQueue;
}
/**
* @param req
* @param <T>
*/
public <T> void addToRequestQueue(Request<T> req) {
req.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}
}
进行了更深入的调试,问题出在服务器上:
this.remoteHostname = !inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()?inetAddress.getHostName().toString():"localhost";
这会生成需要几秒钟的 DNS 查找。
库中已经上传了一个问题 github 项目: