2017-10-12 1 views
0

簡単なHTTPリクエストで、このエラー(BEGIN_ARRAYは期待されていましたが、1行目の2行目はBEGIN_OBJECTでした)を更新することに新しくなりました。ここでcode.HelpがRETROFIT 2予想されるBEGIN_ARRAYでしたが、1行目のBEGIN_OBJECTでした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; 
} 

}

アダプタ

活性

メインをme..`あります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を検索

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; 
} 
} 

が本当に

答えて

0

を理解されるであろう、あなたが持っている設定は、JSONを解析するためのセットアップ改造を持っているようです、この答えはそれも仮定しています。

エラーはGsonであり、それを見ればExpected BEGIN_ARRAY but was BEGIN_OBJECTとなります.jsonの解析中に配列が必要ですが、オブジェクトがあることを伝えています。

エラーの2番目の部分はわかりやすいです。あなたのjsonを見ると、オブジェクトであり配列ではない{で始まります(これは[で始まります)。それで、なぜそれが配列を期待していますか?

このためには、あなたのレトロフィットインターフェイスの宣言に目を向ける必要があります。あなたの呼び出しがList<Volumes>を返します(Gsonはjson配列とJavaリストの間で変換できます)。問題は、返されたjsonは(すでに説明したように)オブジェクトでありリストではないため、gsonはそれをリストに変換するのに問題があるということです。

モデルをさらに見て、それを変更してただVolumesを返すだけでは、それ以上のエラーが発生します。あなたは基本的にjsonをjavaオブジェクトに直接マップする必要があります(あなたが非常に不必要なカスタムデシリアライザを使用しない限り)。

Javaに直接マッピングすると、ルートjson要素を表すオブジェクト、次にアイテムを表すオブジェクト(オブジェクトのリストになる可能性があります)などが必要になります。

ここでは、私が何を意味するかを理解するだけでなく、あなたのjsonに基づいてモデルを生成するのに役立つ素晴らしいwebsiteです。フィールドにjsonを貼り付けて、ソースタイプjson、アノテーションスタイルgsonなど適切なオプションが選択されていることを確認します。アンドロイドスタジオ用のプラグインもあります。

希望すると助かります

関連する問題