我使用库enter link description here
我的班级和方法:
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
Log.d("ionmy", e.toString());
Toast.makeText(getActivity(), result.get(0).name, Toast.LENGTH_LONG).show();
}
});
}
通过这种方式我得到了数据:
[{"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"}]
我的错误:
D/ionmy﹕ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
和stacktrase log:
12-22 07:44:02.138 1015-1015/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:53)
at com.testlist.pavel.transportercity.Fragments.Kitchen_list_of_orders$1.onCompleted(Kitchen_list_of_orders.java:48)
at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:79)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:119)
at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:217)
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)
我无法正确配置JsonObject?
答案 0 :(得分:1)
异常表示正在返回JsonObject,而不是JsonArray(List)。