2017-07-30 6 views
0

た:

[ 
    { 
    "userId": 1, 
    "id": 1, 
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", 
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" 
    }, 
    { 
    "userId": 1, 
    "id": 2, 
    "title": "qui est esse", 
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" 
    } 
] 

私は.Iは、ユーザーのリストを取得しようとしていますRetrofit 2.xを使用して、このJSONを消費しようとしています。ここで

は私APIService interface次のとおりです。ここで

public interface APIService { 

    @POST("posts") 
    Call<List<FakeJSON>> getItems(); 
} 

は、私のPOJOクラスです:最後にここ

public class FakeJSON { 

    @SerializedName("userId") 
    @Expose 
    private int userId; 
    @SerializedName("id") 
    @Expose 
    private int id; 
    @SerializedName("title") 
    @Expose 
    private String title; 
    @SerializedName("body") 
    @Expose 
    private String body; 

    /** 
    * No args constructor for use in serialization 
    * 
    */ 
    public FakeJSON() { 
    } 

    /** 
    * 
    * @param id 
    * @param body 
    * @param title 
    * @param userId 
    */ 
    public FakeJSON(int userId, int id, String title, String body) { 
     super(); 
     this.userId = userId; 
     this.id = id; 
     this.title = title; 
     this.body = body; 
    } 

    public int getUserId() { 
     return userId; 
    } 

    public void setUserId(int userId) { 
     this.userId = userId; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getBody() { 
     return body; 
    } 

    public void setBody(String body) { 
     this.body = body; 
    } 

} 

は私がネットワーク呼び出しを作っています、私のActivityコードです:

public class NetworkActivity extends AppCompatActivity { 

    private static Retrofit.Builder builder = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_network); 
     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     builder = new Retrofit.Builder() 
       .baseUrl("https://jsonplaceholder.typicode.com/") 
       .addConverterFactory(GsonConverterFactory.create()); 

     Retrofit retrofit = 
       builder 
         .client(
           httpClient.build() 
         ) 
         .build(); 
     APIService apiService = retrofit.create(APIService.class); 

     Call<List<FakeJSON>> listCall = apiService.getItems(); 

     listCall.enqueue(new Callback<List<FakeJSON>>() { 
      @Override 
      public void onResponse(Call<List<FakeJSON>> call, Response<List<FakeJSON>> response) { 
       try { 
        Log.e("List Size", response.body().size()+""); 
       }catch (NullPointerException e){ 
        e.printStackTrace();; 
       } 
      } 

      @Override 
      public void onFailure(Call<List<FakeJSON>> call, Throwable t) { 
       Log.e("Error", t.getMessage()); 
      } 
     }); 
    } 
} 

私がアプリケーションを実行しているときは、次の例外が発生します:

E/Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

私はここで間違っていますか?

+1

これを確認してください:https://stackoverflow.com/a/38351872/6021469 –

+0

すでにこれを見ました。私のために働かなかった:( – XoXo

+0

それは私の間違いでした。私は '@ GET'の代わりに' @ POST'を行いました:) – XoXo

答えて

1

'E/Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT

これは改造は、配列の代わりにJSONオブジェクトに直面していることを意味します。 サーバーから何らかのエラーが発生している可能性がありますか? 手動で同じクエリを実行し、応答を確認してください。 コードから行われたリクエストに何か問題があるかどうかを調べることができます。

+0

それは私の間違いでした。 '@ GET'の代わりに' @ POST'を実行しました:) – XoXo

関連する問題