Question

I am calling the Volley but there is a ClassCast exception occurring. Below i have shared the code.

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());            

//      StringRequest request = new StringRequest(Method.GET, "http://headers.jsontest.com/", listener, errorListener);

        String url = "http://echo.jsontest.com/key/value";

         JsonObjectRequest jsObjRequest = new JsonObjectRequest(url, null, listener, errorListener);
        requestQueue.add(jsObjRequest);

Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("","Success Response: " + response.toString()); 
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error.networkResponse != null) {
                Log.d("","Error Response code: " +  error.networkResponse.statusCode);
            }
        }
    };

Why am i getting this exception and how to resolve this exception.

05-08 16:02:57.116: E/AndroidRuntime(16845): java.lang.ClassCastException: java.io.FileInputStream cannot be cast to java.io.ObjectInputStream
05-08 16:02:57.116: E/AndroidRuntime(16845):    at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:398)
05-08 16:02:57.116: E/AndroidRuntime(16845):    at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:157)
05-08 16:02:57.116: E/AndroidRuntime(16845):    at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:85)

/**
         * Reads the header off of an InputStream and returns a CacheHeader object.
         * @param is The InputStream to read from.
         * @throws IOException
         */
        public static CacheHeader readHeader(InputStream is) throws IOException {
            CacheHeader entry = new CacheHeader();

            int magic = readInt(is);
            if (magic != CACHE_MAGIC) {
                // don't bother deleting, it'll get pruned eventually
                throw new IOException();
            }
            entry.key = readString(is);
            entry.etag = readString(is);
            if (entry.etag.equals("")) {
                entry.etag = null;
            }
            entry.serverDate = readLong(is);
            entry.ttl = readLong(is);
            entry.softTtl = readLong(is);
            **entry.responseHeaders = readStringStringMap((ObjectInputStream) is);**                                            return entry;
        }

The italics part in the code which in Volley which is wrong why it wrong.

Was it helpful?

Solution

The exception you get is because you are trying to cast a FileInputStream instance to ObjectInputStream, but this is not the way it works, you have to wrap it:

    final ObjectInputStream os = new ObjectInputStream(is);
    entry.responseHeaders = readStringStringMap(os);

The exception says that your is parameter is actually a FileInputStream, but you don't have to deal with that: it's enough to know that it is an InputStream implementation, so you can use it in the ObjectInputStream constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top