-1

我使用我的图书馆项目在此处输入链接描述

作者举了一个例子:

public static class Tweet {
    public String id;
    public String text;
    public String photo;
}

public void getTweets() throws Exception {
    Ion.with(context)
    .load("http://example.com/api/tweets")
    .as(new TypeToken<List<Tweet>>(){})
    .setCallback(new FutureCallback<List<Tweet>>() {
       @Override
        public void onCompleted(Exception e, List<Tweet> tweets) {
          // chirp chirp
        }
    });
}

我确实效法他的榜样。但尚不清楚示例中的数据以何种形式出现。我举个例子:

public static class Test {
        public String name;
        public String soname;
        public String age;
        public String country;
    }
    private void setData(){
        Ion.with(getActivity())
                .load("http://........")
                .as(new TypeToken<List<Test>>(){})
                .setCallback(new FutureCallback<List<Test>>() {
                    @Override
                    public void onCompleted(Exception e, List<Test> result) {
                        // do stuff with the result or error
                        Toast.makeText(getActivity(), result.get(0).name, Toast.LENGTH_LONG).show();
                    }
                });
    }

但得到一个错误:

12-22 05:46:59.609      414-414/com.testlist.pavel.transportercity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.testlist.pavel.transportercity.Fragments.Kitchen_list_of_orders$1.onCompleted(Kitchen_list_of_orders.java:49)
            at com.testlist.pavel.transportercity.Fragments.Kitchen_list_of_orders$1.onCompleted(Kitchen_list_of_orders.java:45)
            at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:79)
            at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:105)
            at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:215)
            at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:171)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

帮助理解是什么问题?我从服务器的回答:

{"name":"Vasya","soname":"Pupkin","age":"25","country":"Russian Federation"}{"name":"Iliya","soname":"Strelnikov","age":"43","country":"Kazahstan"}

也许不是正确的数据格式和库他们不能理解?

4

1 回答 1

0

图书馆Ion希望JsonArray从 url 收到一个。确保你得到JsonArray. 其次有时result.get(0).name会返回null pointer,原因是应该首先检查请求期间是否发生任何错误。这可以通过方法中的参数e找到onCompleted。然后你需要List在将它发送到 toast 之前检查是否有元素,因为如果0th element是,not found那么你会得到一个空指针错误。

在方法中做类似下面的事情onCompleted

@Override
public void onCompleted(Exception e, List<Test> result) {

   //do stuff with the result or error
   String msg;
   if(e != null) {
       msg = "Error occured";
   }else if(result.size()>0){
       msg = "Received 0 elements";
   }else{
       msg = result.get(0).name;
   }

   Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
于 2014-12-22T06:02:33.343 回答