【发布时间】:2017-05-12 03:10:34
【问题描述】:
我知道json对象以{字符串以“开头”开头。我检查了多次,每个字段的数据类型都与json响应匹配。
我尝试了相同的代码
- 改造
- 异步 HTTP 客户端
这两种情况我都失败了。
我还检查了多个链接,但没有成功。几个链接:
安卓代码:
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;
}
}
POJO 项目
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
【问题讨论】:
-
这一行
System.out.println("-- response = " + response);在日志中打印什么? -
单行 Json 响应。由于行长,部分json响应在android监视器中被削减。
标签: android retrofit2 asynchttpclient gson