即使将超时间隔设置为1分钟,我也会在凌空库上遇到一个奇怪的超时问题。它以前运行正常,但从最近几天它总是显示服务器超时错误。
请查看下面的代码段:
public class AppVolley {
private static AppVolley instance;
private static RequestQueue mRequestQueue = null;
private static ImageLoader imageLoader = null;
private static Context mContext = null;
private AppVolley(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
// Initialize Telkom volley
/**
* Initialize Telkom volley
* @param context of Application
* @return
*/
public static AppVolley getInstance(Context context) {
if(instance == null) {
instance = new AppVolley(context);
} else {
throw new IllegalStateException("Developer forget to initialize request queue");
}
return instance;
}
// Return single request queue through out application
/**
* Return single request queue through out application
* @return request queue of volley
*/
// public static RequestQueue getRequestQueue() {
// return mRequestQueue;
// }
public static RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be
// created when it is accessed for the first time
if (mRequestQueue == null) {
BasicHttpParams mHttpParams = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 60*1000;
HttpConnectionParams.setConnectionTimeout(mHttpParams,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 60*1000;
HttpConnectionParams.setSoTimeout(mHttpParams, timeoutSocket);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory
.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(
mHttpParams, schemeRegistry);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(cm,
mHttpParams);
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext(), new HttpClientStack(defaultHttpClient));
}
return mRequestQueue;
}
// Return same image Loader through out application
/**
* Return Volley image loader instance
* @return
*/
public ImageLoader getImageLoader() {
return imageLoader;
}
}
我的调用函数是
private void initPreLoginSync(LoginResponse loginResponse)
{
RequestQueue mRequestQueue = AppVolley.getRequestQueue();
JsonGenericVolleyRequest<GenericResponse> preSyncRequest = new JsonGenericVolleyRequest<GenericResponse>(
AppSharedPreferences.getInstance(getActivity()).getSalfeldServerName() + AppConstants.WS_PRE_SYNC,
myData,
createSyncSuccessListener(),
createSyncErrorListener(),
GenericResponse.class);
// Help to cancel web-service if required
preSyncRequest.setTag(AppConstants.TAG_WS_PRESYNC);
mRequestQueue.add(preSyncRequest);
}