【发布时间】:2017-02-03 16:19:11
【问题描述】:
我正在尝试在课堂上检索此内容,但出现错误 Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 我的网络服务响应是 {"last_question":"0","level":"0","error":"0"} 谢谢你
json
{"last_question":"0","level":"0","error":"0"}
活动
//calling from MainActivity
private void get()
{
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
API mApi = retrofit.create(API.class);
Call<Example> call = mApi.getdata();
call.enqueue(new Callback<Example>()
{
@Override
public void onResponse(Call<Example> call, Response<Example> response)
{
System.out.println(response.body().getError());
}
@Override
public void onFailure(Call<Example> call, Throwable t)
{
System.out.println(t.getMessage());
}
});
}
//pojo类
public class Example {
private String last_question;
private String level;
private String error;
//all getter andd setter are is here
}
【问题讨论】:
-
应为 BEGIN_OBJECT 但为 STRING 检查您的回复。似乎是字符串,而您的代码需要
JSONObject。您的代码看起来不错 -
我在api.myjson.com/bins/cw29l 托管了相同的 json 以进行测试,并且相同的 android 代码可以工作,除了它的 get 请求和用于接受 application/json 的标头内容类型