RETROFIT 2预计BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列
我是新来改造和获取此错误(预计BEGIN_ARRAY,但BEGIN_OBJECT第1行第2列)在简单的HTTP获取请求。这里是code.Help me..`RETROFIT 2预计BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列
主要活动
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pagination_list);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
String apiKey = getResources().getString(R.string.API_KEY);
GitHubClient client = retrofit.create(GitHubClient.class);
Call<List<Volumes>> call = client.reposForUser(apiKey);
call.enqueue(new Callback<List<Volumes>>() {
@Override
public void onResponse(Call<List<Volumes>> call, Response<List<Volumes>> response) {
List<Volumes> repos = response.body();
int responseCode = response.code();
Log.v("Volumeinfo", "onResponse: "+ responseCode);
listView.setAdapter(new GitHubRepoAdapter(MainActivity.this, repos));
}
@Override
public void onFailure(Call<List<Volumes>> call, Throwable t) {
Log.v("Volumeinfo", "onResponse: "+ t.getMessage());
Toast.makeText(MainActivity.this, t.getMessage()+"error :(", Toast.LENGTH_LONG).show();
}
});
}
接口
{
@GET("volumes?q=Android+intitle")
Call<List<Volumes>> reposForUser(@Query("key")String ApiKey);
}
public class Volumes {
@SerializedName("title")
@Expose
private String title;
public String getTitle() {
return title;
}
}
适配器
private Context context;
private List<Volumes> values;
public GitHubRepoAdapter(Context context, List<Volumes> values) {
super(context, R.layout.list_item_pagination, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_pagination, parent, false);
}
TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text);
Volumes item = values.get(position);
String message = item.getTitle();
textView.setText(message);
return row;
}
}
的Json
{
"kind": "books#volumes",
"totalItems": 3395,
"items": [
{
"kind": "books#volume",
"id": "1igDDgAAQBAJ",
"etag": "oS4LeBsRcfg",
"selfLink": "https://www.googleapis.com/books/v1/volumes/1igDDgAAQBAJ",
"volumeInfo": {
"title": "Android Programming",
"subtitle": "The Big Nerd Ranch Guide",
"authors": [
"Bill Phillips",
"Chris Stewart",
"Kristin Marsicano"
],
"publisher": "Pearson Technology Group",
"publishedDate": "2017-01-30",
"description": "This is the eBook of the printed book and may not include
",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9780134706078"
},
{
"type": "ISBN_10",
"identifier": "0134706072"
}
],
"readingModes": {
"text": true,
"image": true
},
"pageCount": 624,
"printType": "BOOK",
"categories": [
"Computers"
],
"maturityRating": "NOT_MATURE",
"allowAnonLogging": true,
"contentVersion": "1.1.1.0.preview.3",
"panelizationSummary": {
"containsEpubBubbles": false,
"containsImageBubbles": false
},
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?
id=1igDDgAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?
id=1igDDgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.com/books?
完整的Json https://www.googleapis.com/books/v1/volumes?q=Android+intitle
几乎找遍..help将非常感激
您有安装似乎已安装改造解析JSON,所以这个答案也假设了这一点。
你得到的错误来自Gson
,如果你看看它 - Expected BEGIN_ARRAY but was BEGIN_OBJECT
- 它告诉你,当解析json时期望一个数组,但得到了一个对象。
错误的第二部分很容易理解。如果你看看你的json,它开始于{
这是一个对象,而不是一个数组(这将开始于[
)。那么,为什么期待一个数组?
为此,您必须转向您的改造界面声明。你说你的电话会返回一个List<Volumes>
(Gson可以在json数组和java列表之间转换)。问题是返回的json(如前所述)是一个对象而不是一个列表,所以gson无法将其转换为列表。
再看看您的模型,将其更改为仅返回Volumes
将是不够的,将导致更多的错误。你必须基本上直接将你的json映射到java对象(除非你想使用非常不必要的自定义反序列化器)。
通过直接映射到java我的意思是你必须拿出一个代表根json元素的对象,然后代表项目(它可以只是一个对象列表)等等。
下面是一个很棒的website,它不仅可以帮助您理解我的意思,还可以根据您的json为您生成模型。你将json粘贴到字段中,确保你选择了正确的选项 - 源类型json,注释样式gson等。甚至还有android studio的插件。
希望它有帮助