Search code examples
androidjsonandroid-ionion-koush

Ion-koush - Error when i want to get JsonArray


I'm going to use Ion library in my project. An api that I'm using is like this: API link

I wrote this based on ion-sample twitter:

Future<JsonArray> VideoLoad;
    private void load(){

        String MostWatched_Url      = "http://www.aparat.com/etc/api/mostviewedvideos/perpage/20";

        // don't attempt to load more if a load is already in progress
        if (VideoLoad != null && !VideoLoad.isDone() && !VideoLoad.isCancelled()) return;

        VideoLoad = Ion.with(this, MostWatched_Url)
                .setLogging("Parsvid", Log.DEBUG)
                .asJsonArray()
                .setCallback(new FutureCallback<JsonArray>() {

                    @Override
                    public void onCompleted(Exception e, JsonArray result) {
                        for (int i = 0; i < result.size(); i++) {
                            videoAdapter.add(result.get(i).getAsJsonObject());
                        }
            }
        });
    }

But when i run this I get this error:

05-03 13:58:27.258: D/Parsvid(19020): (629 ms) http://www.aparat.com/etc/api/mostviewedvideos/perpage/20: Caching response
05-03 13:58:27.258: D/Parsvid(19020): (630 ms) http://www.aparat.com/etc/api/mostviewedvideos/perpage/20: Connection successful
05-03 13:58:27.318: D/AndroidRuntime(19020): Shutting down VM
05-03 13:58:27.318: W/dalvikvm(19020): threadid=1: thread exiting with uncaught exception (group=0x417e1c08)
05-03 13:58:27.323: D/Parsvid(19020): (691 ms) http://www.aparat.com/etc/api/mostviewedvideos/perpage/20: Recycling keep-alive socket
05-03 13:58:27.323: E/AndroidRuntime(19020): FATAL EXCEPTION: main
05-03 13:58:27.323: E/AndroidRuntime(19020): Process: com.may3am.parsvid, PID: 19020
05-03 13:58:27.323: E/AndroidRuntime(19020): java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.may3am.parsvid.MainActivity$2.onCompleted(MainActivity.java:1)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:96)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:130)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:117)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:239)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:53)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at android.os.Handler.handleCallback(Handler.java:733)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at android.os.Looper.loop(Looper.java:136)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at android.app.ActivityThread.main(ActivityThread.java:5476)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at java.lang.reflect.Method.invokeNative(Native Method)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at java.lang.reflect.Method.invoke(Method.java:515)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-03 13:58:27.323: E/AndroidRuntime(19020):    at dalvik.system.NativeStart.main(Native Method)

Where is my bad?! I did it as like as ion-sample, of course my api don't need to authenticating... and i dont set header for my request.


Solution

  • What you receive is not an array, but a single object, that contains an array as a value of the attribute mostviewedvideos:

    {"mostviewedvideos":[....],
     "ui":{...}}
    

    That is what your error is saying (object can not be cast to an array). If it were an array it would have started with square brackets []