2016-05-07 14 views
0

この問題を解決するには2つのアプローチを試みましたが、どちらもうまくいかず、何が起こったのか分かりません。 私は、次のJSONを持っている:Retrofit Gson:予想通りBEGIN_ARRAYでしたが、BEGIN_OBJECT(重複していません)

public interface PostService { 
    @Headers("Content-Type:application/json") 
    @GET("/posts") 
    Call<List<Post>> getAllPosts(); 

    @Headers("Content-Type:application/json") 
    @GET("users/{id}/postsesByUserid") 
    Call<List<Post>> getUsersPost(@Path("id") int id); 
} 

しかし、私はこの問題を得る:

Retrofit retrofit = RetrofitGenerator.initRetrofit(CacheUtils.readSharedPreference(getActivity()).getToken()); 
     PostService postService= retrofit.create(PostService.class); 
     postService.getUsersPost((int) CacheUtils.readSharedPreference(getActivity()).getUserID()).enqueue(new Callback<List<Post>>() { 
      @Override 
      public void onResponse(Call<List<Post>> call, Response<List<Post>> response) { 
       if(response.isSuccessful()){ 
        List<Post> postEntitiyList = response.body(); 

       } 
      } 

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

とインタフェース:

{ 
    "_embedded" : { 
    "posts" : [ { 
     "postid" : 1, 
     "taggedusers" : null, 
     "hashtags" : null, 
     "createdAt" : "2016-05-07", 
     "commentsCount" : 0, 
     "likesCount" : 0, 
     "shareCount" : 0, 
     "description" : "nothing", 
     "post" : "nothing", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/posts/1" 
     }, 
     "postsEntity" : { 
      "href" : "http://localhost:8080/posts/1" 
     }, 
     "usersByUserid" : { 
      "href" : "http://localhost:8080/posts/1/usersByUserid" 
     }, 
     "commentsesByPostid" : { 
      "href" : "http://localhost:8080/posts/1/commentsesByPostid" 
     } 
     } 
    }, { 
     "postid" : 2, 
     "taggedusers" : null, 
     "hashtags" : null, 
     "createdAt" : "2016-05-07", 
     "commentsCount" : 0, 
     "likesCount" : 0, 
     "shareCount" : 0, 
     "description" : "nothing", 
     "post" : "nothing", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/posts/2" 
     }, 
     "postsEntity" : { 
      "href" : "http://localhost:8080/posts/2" 
     }, 
     "usersByUserid" : { 
      "href" : "http://localhost:8080/posts/2/usersByUserid" 
     }, 
     "commentsesByPostid" : { 
      "href" : "http://localhost:8080/posts/2/commentsesByPostid" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8080/users/6/postsesByUserid" 
    } 
    } 
} 

は私が改造に次のように使用してそれを読み込もうとしましたが予想されますBEGIN_ARRAYでしたが、1行目の2列目のパスでBEGIN_OBJECTでした$ Post Pojoを次のような別のクラスに作成しようとしました:

public class PostEntitiy { 
    private Post post; 

    public PostEntitiy(Post post) { 
     this.post = post; 
    } 

    public Post getPost() { 
     return post; 
    } 

    public void setPost(Post post) { 
     this.post = post; 
    } 
} 

次に、リストを使用しても、私は同じ問題を抱えていますが、私が使っている間違ったことは何ですか?

答えて

2

BEGIN_ARRAY期待が、あなたのJSONが、それは、オブジェクトのリストではありませんBEGIN_OBJECT

た:{ ... } =>オブジェクト、[ ... ] =>リスト。

EDIT

あなたが始めるために:

class Embedded { 
    ... 
} 

class Links { 
    ... 
} 

class Top { 
    Embedded _embedded; 
    Links _links; 
} 

などなどを。

List<Post>のすべてをTopに置き換えてください。

自分のスタイルに合わせてクラスの名前を変更します。

+0

どうすれば読むことができますか?お願いします? _embedクラスを使用する必要があります –

+0

はい、JSONの名前と型に一致する属性を持つJavaオブジェクトの階層を設計する必要があります。 – totoro

+0

@ H-Ideas私はアイデアを始めて編集しました。私は改造に精通していないので、私ができることは最高です:-) – totoro

関連する問題