从Json Expect Array改进映射并获取Object

时间:2016-12-28 13:15:41

标签: android json retrofit

我有Json:

public static Retrofit getClient() {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
                .setLenient() .create();
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }

模特课:

public static void getPerson(Callback<List<Person>> callback, Context context, Map<String,String> params){
        mContext=context;
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        Map<String,String> queryMap = getQueryMap(LAST_MUSEUM_EVENT_CALL_TIMESTAMP,params);
        Call<List<Person>> call= apiService.getPerson(queryMap);
        call.enqueue(callback);

    }

API客户端

ApiHelper.getPerson(new Callback<List<Person>>() {
                    @Override
                    public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {



                      List<Person> mlist = response.body();


                    }
                    @Override
                    public void onFailure(Call<List<Person>> call, Throwable t) {
                        Log.i(TAG,"ERROR"+ t.getMessage());
                    }
                },MainActivity.this,options);

助手班

public class GuaranteedDeadlock {
    public static void main(String[] args) {
        long[] arr = new long[Integer.MAX_VALUE];
    }
}

当我做出回应时,我的主要活动部分:

Exception in thread "main" java.lang.ClassNotFoundException: GuaranteedDeadlock
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

我的请求的网址是正确的,但我有这样的错误:

  

预计BEGIN_ARRAY,但在第1行第2栏路径为BEGIN_OBJECT $

1 个答案:

答案 0 :(得分:0)

您的JSON响应格式不正确。

{
   "data":[
        "name": "name1",
        "surname": "surname1",
        "id": "22",
        "motivations": []
    },
    "1:"{
        "name": "name2",
        "surname": "surname2",
        "id": "23",
        "motivations": []
    },
    {
        "name": "name3",
        "surname": "surname3",
        "id": "24",
        "motivations": []
    }],
    "sign": "9e46b7d6b140b",
    "last_call": 1446
}

在“数据”之后,您有一个标记数组开头的左括号。但是在数组结束之前,你有一个结束对象的结束花括号。那里出了点问题。