简体   繁体   English

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

[英]Ion-koush - Error when i want to get JsonArray

I'm going to use Ion library in my project. 我将在我的项目中使用Ion库。 An api that I'm using is like this: API link 我正在使用的api是这样的: API链接

I wrote this based on ion-sample twitter: 我是根据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. 我做的就像离子样本一样,当然我的api不需要进行身份验证......而且我没有为我的请求设置标题。

What you receive is not an array, but a single object, that contains an array as a value of the attribute mostviewedvideos : 你收到的不是一个数组,而是一个单独的对象,它包含一个数组作为属性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 [] 如果它是一个数组,它将以方括号[]开头

I agree with Boris. 我同意鲍里斯的观点。 in JSON everything you are getting under JSONObject. 在JSON中你在JSONObject下得到的一切。

Here is a sample project to help you with. 是一个帮助您的示例项目。 This project has the particular code for getting JSONArrays from the server and convert it to java object using GSON. 该项目具有从服务器获取JSONArrays并使用GSON将其转换为java对象的特定代码。

You can fork it from github and reuse the already tested code for Ion Library. 您可以从github派生它并重用已经测试过的Ion Library代码。

Check this out. 看看这个

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM