Ion-koush - 当我想获得JsonArray时出错

时间:2014-05-03 09:37:48

标签: android json ion ion-koush

我将在我的项目中使用Ion库。我正在使用的api是这样的: API link

我是根据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());
                        }
            }
        });
    }

但是当我运行这个时,我得到了这个错误:

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)

我的哪个不好?! 我做的就像离子样本一样,当然我的api不需要进行身份验证......我也没有为我的请求设置标题。

2 个答案:

答案 0 :(得分:4)

您收到的不是数组,而是单个对象,其中包含一个数组作为属性mostviewedvideos的值:

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

这就是您的错误所说的内容(无法将对象强制转换为数组)。如果它是一个数组,它将以方括号[]

开头

答案 1 :(得分:-2)

我同意鲍里斯的观点。在JSON中你在JSONObject下得到的一切。

Here是一个帮助您的示例项目。该项目具有从服务器获取JSONArrays并使用GSON将其转换为java对象的特定代码。

您可以从github派生它并重用已经测试过的Ion Library代码。

检查this

希望它有所帮助。