Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API declarations must be interfaces- Retrofit

I am try to do simple get response using Retrofit. And i have error java.lang.IllegalArgumentException: API declarations must be interfaces.. I can not figure out what exactly the problem. I have this error when i try to do CategoryJsonParser categoryParsed = retrofit.create(CategoryJsonParser.class);.

So its my interface:

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

How i create retrofit in main act:

 //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

Here if my 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;
    }

}

And its my 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;
    }

}

Error:

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)

Iam seriously cant understant wats wrong. Can you help me guys?

like image 982
Дмитрий Дьяченко Avatar asked Jan 11 '17 13:01

Дмитрий Дьяченко


People also ask

What is retrofit in API?

Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.

What is retrofit API in Android?

What is Retrofit? Retrofit is a REST Client library (Helper Library) used in Android and Java to create an HTTP request and also to process the HTTP response from a REST API. It was created by Square, you can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson.

What is retrofit in Android with example?

Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. To serialise JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our build. grade file.

How to create retrofit API in Android app?

Navigate to the app > java > your app’s package name > Right-click on it > New > Java class select it as Interface and name the file as RetrofitAPI and add below code to it. Comments are added inside the code to understand the code in more detail. Go to the MainActivity.java file and refer to the following code.

Should retrofit be an interface or a class?

Yeah, it should be an interface. InterfaceForCategory categoryService = retrofit.create (InterfaceForCategory.class); // this line is good categoryService.getData ().enqueue (new Callback<> () {...}); But I'm pretty sure the official documentation explains this sufficiently well.

How to call REST API using retrofit in flutter?

To call Rest API’s by sending dynamic headers, parameters, request & response in a custom and secured way “Retrofit” is the best way. Step 2: To work with Retrofit implementation in flutter we need to add below dependencies in pubspec.yaml file. Dio is our Http client and handling the connection for us.

How to add comments in retrofit API?

Comments are added inside the code to understand the code in more detail. Navigate to the app > java > your app’s package name > Right-click on it > New > Java class select it as Interface and name the file as RetrofitAPI and add below code to it. Comments are added inside the code to understand the code in more detail.


1 Answers

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

Yeah, it should be an interface.

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

But I'm pretty sure the official documentation explains this sufficiently well.

like image 140
EpicPandaForce Avatar answered Sep 22 '22 01:09

EpicPandaForce