我尝试使用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)
Iam严重不能轻信错误。你能帮我吗?
最佳答案
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。