0

I am facing one weird time out issue on volley library even after setting timeout interval to 1 min. It was running fine previously but from last couple of days it always show server time out error.

Please take a look of my code snippet below:

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;
    }

}

and my calling function are as

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);

    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Karna
  • 1
  • 5
  • Check your internet connection – John Feb 27 '15 at 06:54
  • I think you just check out with your severs connection the problem is frm their side – Born To Win Feb 27 '15 at 07:14
  • http://stackoverflow.com/a/22169775/1081340 – Vrashabh Irde Feb 27 '15 at 07:31
  • Thanks for reply guys. But my concern is that using retry policy will increase no of hits at server side. Let suppose out of my thousands of user if 500 hundreds are calling server at a time than chances of hits at server will be 500 * 2 if i have set my retries = 1. If i am not wrong. – Karna Feb 27 '15 at 07:51

0 Answers0