2017-10-12 135 views
0

我是新來改造和獲取此錯誤(預計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將非常感激

回答

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的插件。

希望它有幫助

相關問題