改装2预期BEGIN_ARRAY但在第1栏第2列是BEGIN_OBJECT

时间:2017-10-12 00:12:10

标签: java android-studio networking httprequest retrofit2

我是新手,在简单的HTTP get请求中进行改造并获得此错误(预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT)。这是代码。帮助我......

主要活动

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

几乎到处搜索..帮助将非常感激

1 个答案:

答案 0 :(得分:0)

您所拥有的设置似乎已经设置了改进来解析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的插件。

希望有所帮助