Problem with Volley NegativeArraySizeException

1

I'm doing an app that has several calls get using Volley (both string and JSON) and unpredictably (I can not tell when it's going to jump) I skip the following error:

java.lang.NegativeArraySizeException: -603
at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:323) 
at com.android.volley.toolbox.DiskBasedCache.get(DiskBasedCache.java:119)
at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:100)

I also give an example of one of my calls:

protected void ponerFondo(final RelativeLayout abl) {
   //la razón de que aquí haya un handler es para pedir dos veces la misma consulta, ya que a veces no coge bien el string a la primera
    recargar.postDelayed(new Runnable() {
        @Override
        public void run() {
            recargar.removeCallbacksAndMessages(null);
            ponerFondo(abl);
        }
    }, 800);
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    String StringURL = "url";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, StringURL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    btn12 = (ImageButton) findViewById(R.id.btn12);
                    String imagenes[];
                    imagenes = response.split("#");

                    Picasso.with(getApplicationContext()).load(imagenes[0]).placeholder(abl.getBackground()).into(new Target(){


                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            abl.setBackground(new BitmapDrawable(bitmap));
                        }

                        @Override
                        public void onBitmapFailed(final Drawable errorDrawable) {
                            ponerFondo(abl);
                        }

                        @Override
                        public void onPrepareLoad(final Drawable placeHolderDrawable) {
                            // Log.d("TAG", "Prepare Load");
                        }
                    });
                    if(!imagenes[1].equals("null")) {
                        Picasso.with(getApplicationContext()).load(imagenes[1]).into(btn12);
                        btn12.setVisibility(View.VISIBLE);
                    }





                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            ponerFondo(abl);
        }
    });

    requestQueue.add(stringRequest);
}
    
asked by Jose Antonio Moral 25.07.2017 в 12:49
source

1 answer

1

As for NegativeArraySizeException , I can tell you that when you define and you dimension an Array you obviously assign positive values, but what would happen if you assign a negative value ?, example:

   int arrayEnteros[]=new int[-5];
   for(int i = 0; i < 5; i ++){
      arrayEnteros[i] = i; //Asigna valores al array.
   }
   System.out.println("Valor del primer elemento del array: " + arrayEnteros[0]);

when trying to print the value inside the array you would get:

Exception java.lang.NegativeArraySizeException

In case you get NegativeArraySizeException using < a href="https://developer.android.com/training/volley/index.html"> Volley , precisely this problem occurs, what you must do is get the size of the cache and avoid operations if this Deliver a negative measure :

File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
DiskBasedCache cache = new DiskBasedCache(cacheDir);
if(cacheFile.length() > 0){ //Mayor a 0
 //Realiza operación.
}

There is a report regarding this: link

    
answered by 25.07.2017 в 16:25