Retrofit2, AsyncHTTPClient — ожидался BEGIN_OBJECT, но был STRING в строке 1, столбце 1, пути $

Я знаю, что объект json начинается с {строка начинается с ". Я проверял несколько раз, типы данных каждого поля совпадают с ответом json.

Я пробовал тот же код с

  1. Модернизация
  2. Асинхронный HTTP-клиент

Я потерпел неудачу в обоих случаях.

Я также проверил несколько ссылок, но безуспешно. Несколько ссылок:

Ссылка 1
Ссылка 2
Ссылка 3

Код Android:

Type type = new TypeToken<AuditionResponse>() {
}.getType();
AuditionResponse auditionResponse = null;
try {
    String response = new String(responseBody, "UTF-8");
    System.out.println("-- response = " + response);
    auditionResponse = new Gson().fromJson(response, type);
} catch (Exception e) {
    e.printStackTrace();
}

Ответ POJO

public class Response {
    String message;

    public Response(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

AuditionResponse POJO

public class AuditionResponse extends Response {
    private List<Project> data;

    public AuditionResponse(String message, List<Project> data) {
        super(message);
        this.data = data;
    }

    public List<Project> getData() {
        return data;
    }

    public void setData(List<Project> data) {
        this.data = data;
    }
}

Проект ПОЖО

public class Project implements Serializable {
    private String id;
    private String user_id;
    private String name;
    private String type;
    private String desc;
    private String location;
    private String country_id;
    private String state_id;
    private String city_id;
    private String roleCount;
    private String applicationCount;
    private String castingDate;
    private String shootDate;
    private String industry;
    private String casting_director;
    private String director;
    private String same_location_actor_can_apply;
    private String is_paid;
    private String status;
    private String created;
    private String modified;
}

Json-ответ:

{
"message": "",
"data": [
    {
        "id": "284",
        "user_id": "7393",
        "name": "Indo-American film.",
        "type": "1",
        "desc": "Soni , a convicted former police officer, serving an unjust four year sentence for\r\nmanslaughter, is haunted by a past that has left her embittered and cynical. Her former boss,\r\nKalpana (Female, 32), IPS, feels a sense of guilt for not backing Soni during her trial, and compensates\r\nby securing her release for a sting operation initially conceived by Soni herself.!\r\nSetting aside their difference of opinions, they agree to embark upon the mission together, but\r\nthings get off to a rough start, when Soni breaks protocol, jeopardizing the legitimacy of the operation.\r\nThe incident uncovers an intense clash of ideas.",
        "location": "Delhi",
        "country_id": "India",
        "state_id": "Delhi",
        "city_id": "Central Delhi",
        "roleCount": "6",
        "applicationCount": null,
        "castingDate": "2016-12-31",
        "shootDate": "1970-01-01",
        "industry": "0",
        "casting_director": "Trishaan Csa",
        "director": "Undisclosed ",
        "same_location_actor_can_apply": "0",
        "is_paid": "1",
        "status": "1",
        "device": "Web",
        "created": "2016-12-22 09:11:00",
        "modified": "2016-12-23 12:59:00"
    },
    {
        "id": "283",
        "user_id": "21193",
        "name": "Romantic fiction show ",
        "type": "3",
        "desc": "A weekly romantic fiction show to be aired on a popular channel. Looking for actors to play lead roles. ",
        "location": "mumbai ",
        "country_id": "India",
        "state_id": "Maharashtra",
        "city_id": "",
        "roleCount": "2",
        "applicationCount": null,
        "castingDate": "2017-01-01",
        "shootDate": "1970-01-01",
        "industry": "0",
        "casting_director": "Jigna ",
        "director": "Undisclosed ",
        "same_location_actor_can_apply": "0",
        "is_paid": "1",
        "status": "1",
        "device": null,
        "created": "2016-12-21 09:42:00",
        "modified": "2016-12-21 09:43:00"
    },
    {
        "id": "282",
        "user_id": "21193",
        "name": "T.v show with a prominent channel ",
        "type": "3",
        "desc": "Its an undisclosed t.v show with a prominent channel, the story revolves around Punjabi culture .Looking for child actors to play lead and supporting roles. It is an continuity track for 8-10 months. ",
        "location": "mumbai ",
        "country_id": "India",
        "state_id": "Maharashtra",
        "city_id": "",
        "roleCount": "3",
        "applicationCount": null,
        "castingDate": "2016-12-30",
        "shootDate": "1970-01-01",
        "industry": "0",
        "casting_director": "Jigna ",
        "director": "Undisclosed",
        "same_location_actor_can_apply": "0",
        "is_paid": "0",
        "status": "1",
        "device": null,
        "created": "2016-12-21 08:02:00",
        "modified": "2016-12-21 08:07:00"
    },
    {
        "id": "280",
        "user_id": "26727",
        "name": "Pal Pal Dil Ke Paas",
        "type": "1",
        "desc": "The tradition of launching the sons of Deol\u00e2\u20ac\u2122s with a romantic film is being continued with Pal Pal Dil Ke Paas.Actor Sunny Deol is once again donning the director\u00e2\u20ac\u2122s hat, and this time he is all set to launch his son Karan Deol as the hero. The team is in Hunt for the cast. This is a golden opportunity for all you young actors out there to enter the world of Bollywood",
        "location": "Mumbai\/ Delhi ",
        "country_id": "India",
        "state_id": "Maharashtra",
        "city_id": "",
        "roleCount": "1",
        "applicationCount": null,
        "castingDate": "2016-12-30",
        "shootDate": "1970-01-01",
        "industry": "0",
        "casting_director": "Henna ",
        "director": "Sunny Deol ",
        "same_location_actor_can_apply": "0",
        "is_paid": "1",
        "status": "1",
        "device": null,
        "created": "2016-12-14 06:49:00",
        "modified": "2016-12-14 06:54:00"
    }

Исключение:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
        at com.google.gson.Gson.fromJson(Gson.java:887)
        at com.google.gson.Gson.fromJson(Gson.java:852)
        at com.google.gson.Gson.fromJson(Gson.java:801)
        at com.actorsapply.actor.auditions.AuditionsFragment$1.onSuccess(AuditionsFragment.java:105)
  //line 105 -- auditionResponse = new Gson().fromJson(response, type);
        at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:351)
        at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5459)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
        at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
        ... 12 more

person Paritosh    schedule 27.12.2016    source источник
comment
Что эта строка System.out.println("-- response = " + response); печатает в логах?   -  person iagreen    schedule 28.12.2016
comment
Ответ Json в одну строку. Часть ответа json урезана в мониторе Android из-за длины строки.   -  person Paritosh    schedule 28.12.2016


Ответы (1)


Я думаю, вы должны использовать такую ​​модель ответа:

public class AuditionResponse {
    private String message;
    private List<Project> data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Project> getData() {
        return data;
    }

    public void setData(List<Project> data) {
        this.data = data;
    }
}
person Billy Bons    schedule 27.12.2016
comment
Я расширяю класс Response для всех объектов ответа. Но этот конкретный веб-сервис терпит неудачу. Есть ли что-нибудь, связанное с кодировкой символов Unicode, которые я должен проверить? - person Paritosh; 28.12.2016