-1

I use my library Projects enter link description here

The author gives an example:

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
        }
    });
}

I do follow his example. But is not clear in what form the data come in Example. I make the example:

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();
                    }
                });
    }

but get an error:

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)

help to understand what is the problem? my answer from the server:

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

Maybe not the correct data format and the library can not they understand?

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Valera Valerianov
  • 247
  • 1
  • 3
  • 14

1 Answers1

0

The Ion library expects to receive an JsonArray from the url. Make sure you get JsonArray. Secondly result.get(0).name would return null pointer sometimes, the reason being one should first check if there is any error happened during the request. This can be found via the parameter e in onCompleted method. Then you need to check if the List has elements before sending it to a toast, coz if 0th element is not found then you are gonna get a null pointer error.

Do something similar to below in onCompleted method.

@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();
}
Panther
  • 8,938
  • 3
  • 23
  • 34
  • 12-22 06:08:41.218 577-577/com.testlist.pavel.transportercity D/ionmy﹕ Error occured the fact that the example does not specify which row gets author. and so I can not understand. – Valera Valerianov Dec 22 '14 at 06:09
  • i could not understand ur comment :-/ – Panther Dec 22 '14 at 06:40
  • java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT – Valera Valerianov Dec 22 '14 at 06:45
  • I want to say that I do not understand how the line should look like that I get from the server. in my case – Valera Valerianov Dec 22 '14 at 06:46
  • well the error shows that it expects an jsonArray while it got was jsonObject. Make sure your url returns jsonArray. An jsonArray should have the format `[{},{},{}]` – Panther Dec 22 '14 at 07:00
  • I try so, but still error [{"name":"Vasya","soname":"Pupkin","age":"25","country":"Russian Federation"},{"name":"Iliya","soname":"Strelnikov","age":"43","country":"Kazahstan"},{"name":"Iliya","soname":"Lagutenko","age":"49","country":"Ukraine"}] – Valera Valerianov Dec 22 '14 at 07:20