API声明必须是接口 - Retrofit

时间:2017-01-11 13:39:26

标签: java android retrofit2

我尝试使用Retrofit进行简单的获取响应。我有错误java.lang.IllegalArgumentException: API declarations must be interfaces.。我无法弄清楚究竟是什么问题。我尝试CategoryJsonParser categoryParsed = retrofit.create(CategoryJsonParser.class);时出现此错误。

所以我的界面:

public interface InterfaceForCategory {
@GET("categories?api_key=l6pdqjuf7hdf97h1yvzadfce")
Call<List<CategoryJsonParser>> getData();
}

我如何在主要行为中创建改造:

 //get retrofit builder
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://openapi.etsy.com/v2/taxonomy/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

CategoryJsonParser categoryParsed = retrofit.create(CategoryJsonParser.class); // this line error

如果我的CategoryJsonParser

@Generated("net.hexar.json2pojo")
@SuppressWarnings("unused")
public class CategoryJsonParser {

    @SerializedName("count")
    private Long mCount;
    @SerializedName("pagination")
    private Pagination mPagination;
    @SerializedName("params")
    private Object mParams;
    @SerializedName("results")
    private List<JsonCategoryParsed> mResults;
    @SerializedName("type")
    private String mType;

    public Long getCount() {
        return mCount;
    }

    public void setCount(Long count) {
        mCount = count;
    }

    public Pagination getPagination() {
        return mPagination;
    }

    public void setPagination(Pagination pagination) {
        mPagination = pagination;
    }

    public Object getParams() {
        return mParams;
    }

    public void setParams(Object params) {
        mParams = params;
    }

    public List<JsonCategoryParsed> getResults() {
        return mResults;
    }

    public void setResults(List<JsonCategoryParsed> results) {
        mResults = results;
    }

    public String getType() {
        return mType;
    }

    public void setType(String type) {
        mType = type;
    }

}

它是我的JsonCategoryParserd

@Generated("net.hexar.json2pojo")
@SuppressWarnings("unused")
public class JsonCategoryParsed {

    @SerializedName("category_id")
    private Long mCategoryId;
    @SerializedName("category_name")
    private String mCategoryName;
    @SerializedName("long_name")
    private String mLongName;
    @SerializedName("meta_description")
    private String mMetaDescription;
    @SerializedName("meta_keywords")
    private String mMetaKeywords;
    @SerializedName("meta_title")
    private String mMetaTitle;
    @SerializedName("name")
    private String mName;
    @SerializedName("num_children")
    private Long mNumChildren;
    @SerializedName("page_description")
    private String mPageDescription;
    @SerializedName("page_title")
    private String mPageTitle;
    @SerializedName("short_name")
    private String mShortName;

    public Long getCategoryId() {
        return mCategoryId;
    }

    public void setCategoryId(Long category_id) {
        mCategoryId = category_id;
    }

    public String getCategoryName() {
        return mCategoryName;
    }

    public void setCategoryName(String category_name) {
        mCategoryName = category_name;
    }

    public String getLongName() {
        return mLongName;
    }

    public void setLongName(String long_name) {
        mLongName = long_name;
    }

    public String getMetaDescription() {
        return mMetaDescription;
    }

    public void setMetaDescription(String meta_description) {
        mMetaDescription = meta_description;
    }

    public String getMetaKeywords() {
        return mMetaKeywords;
    }

    public void setMetaKeywords(String meta_keywords) {
        mMetaKeywords = meta_keywords;
    }

    public String getMetaTitle() {
        return mMetaTitle;
    }

    public void setMetaTitle(String meta_title) {
        mMetaTitle = meta_title;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public Long getNumChildren() {
        return mNumChildren;
    }

    public void setNumChildren(Long num_children) {
        mNumChildren = num_children;
    }

    public String getPageDescription() {
        return mPageDescription;
    }

    public void setPageDescription(String page_description) {
        mPageDescription = page_description;
    }

    public String getPageTitle() {
        return mPageTitle;
    }

    public void setPageTitle(String page_title) {
        mPageTitle = page_title;
    }

    public String getShortName() {
        return mShortName;
    }

    public void setShortName(String short_name) {
        mShortName = short_name;
    }

}

错误:

FATAL EXCEPTION: main
                                                                   Process: pc.dd.vegetables_simple, PID: 4255
                                                                   java.lang.IllegalArgumentException: API declarations must be interfaces.
                                                                       at retrofit2.Utils.validateServiceInterface(Utils.java:309)
                                                                       at retrofit2.Retrofit.create(Retrofit.java:128)
                                                                       at pc.dd.vegetables_simple.Fragments.CategoryFragment.<init>(CategoryFragment.java:42)
                                                                       at pc.dd.vegetables_simple.Adapters.PagerAdapterVeg.getItem(PagerAdapterVeg.java:23)
                                                                       at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:109)
                                                                       at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1006)
                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1154)
                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                       at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                       at android.view.View.measure(View.java:19863)
                                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2270)
                                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6306)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6095)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我真的不能理解错误的错误。你能帮帮我们吗?

3 个答案:

答案 0 :(得分:4)

CategoryJsonParser categoryParsed = retrofit.create(CategoryJsonParser.class); // this line error

是的,它应该是界面

InterfaceForCategory categoryService = retrofit.create(InterfaceForCategory.class); // this line is good
categoryService.getData().enqueue(new Callback<>(){...});

但我很确定the official documentation explains this sufficiently well

答案 1 :(得分:1)

你需要这样的东西:

InterfaceForCategory myService = retrofit.create(InterfaceForCategory.class);

答案 2 :(得分:1)

您的api实现类(GitHubService.class)应该是这里的接口:

GitHubService service = retrofit.create(GitHubService.class);

查看官方改造文档here

或查看示例here

API接口示例:

public interface GitApiInterface {

        @Headers("User-Agent: Retrofit2.0Tutorial-App")
        @GET("/search/users")
        Call<GitResult> getUsersNamedTom(@Query("q") String name);

        @POST("/user/create")
        Call<Item> createUser(@Body String name, @Body String email);

        @PUT("/user/{id}/update")
        Call<Item> updateUser(@Path("id") String id , @Body Item user);
    }