显然,我已经错误地实施了getApi
和/或getActiveListing
方法,无法从Etsy
检索内容。
Etsy.java
import com.joshbgold.recommendations.model.ActiveListings;
import retrofit.Callback;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
public class Etsy {
private static final String API_KEY = "juj938tmfk0nq9zrzc6mhsav";
private static RequestInterceptor getInterceptor() {
return new RequestInterceptor(){
@Override
public void intercept(RequestInterceptor.RequestFacade request){
request.addEncodedQueryParam("api_key", API_KEY);
}
};
}
private static Api getApi(){
return new RestAdapter.Builder()
.setEndpoint("https://openapi.etsy.com/v2")
.setRequestInterceptor(getInterceptor())
.build()
.create(Api.class);
}
public static void getActiveListings(Callback<ActiveListings> callback){
getApi().activeListings("Images, Shop", callback);
}
}
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.joshbgold.recommendations.API.Etsy;
import com.joshbgold.recommendations.model.ActiveListings;
public class MainActivity extends AppCompatActivity {
private static final String STATE_ACTIVE_LISTINGS = "StateActiveListings";
private RecyclerView recyclerView;
private View progressBar;
private TextView errorView;
private ListingAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
progressBar = (findViewById(R.id.progressbar));
errorView = (TextView) findViewById(R.id.error_view);
//setup recyclerview
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
adapter = new ListingAdapter(this);
recyclerView.setAdapter(adapter);
if(savedInstanceState == null){
showLoading();
Etsy.getActiveListings(adapter);
}
else {
if(savedInstanceState.containsKey(STATE_ACTIVE_LISTINGS)) {
adapter.success((ActiveListings) savedInstanceState.getParcelable(STATE_ACTIVE_LISTINGS), null);
showList();
} else{
showLoading();
Etsy.getActiveListings(adapter);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ActiveListings activeListings = adapter.getActiveListings();
if(activeListings != null){
outState.putParcelable(STATE_ACTIVE_LISTINGS, activeListings);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showLoading(){
progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
errorView.setVisibility(View.GONE);
}
public void showList(){
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
errorView.setVisibility(View.GONE);
}
public void showError(){
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
errorView.setVisibility(View.VISIBLE);
}
}
Api.java
import com.joshbgold.recommendations.model.ActiveListings;
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;
public class Api {
@GET("/listings/active")
void activeListings(@Query("includes") String includes,
Callback<ActiveListings> callback) {
}
}
logcat的:
09-11 11:49:39.234 11172-11172/? I/art﹕ Late-enabling -Xcheck:jni
09-11 11:49:39.414 11172-11172/com.joshbgold.recommendations D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-11 11:49:39.416 11172-11172/com.joshbgold.recommendations E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.joshbgold.recommendations, PID: 11172
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshbgold.recommendations/com.joshbgold.recommendations.MainActivity}: java.lang.IllegalArgumentException: Only interface endpoint definitions are supported.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
**Caused by: java.lang.IllegalArgumentException: Only interface endpoint definitions are supported.
at retrofit.Utils.validateServiceClass(Utils.java:102)
at retrofit.RestAdapter.create(RestAdapter.java:193)
at com.joshbgold.recommendations.API.Etsy.getApi(Etsy.java:30)
at com.joshbgold.recommendations.API.Etsy.getActiveListings(Etsy.java:34)
at com.joshbgold.recommendations.MainActivity.onCreate(MainActivity.java:43)**
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-11 11:51:32.053 11172-11184/com.joshbgold.recommendations W/art﹕ Suspending all threads took: 13.000ms
09-11 11:54:39.464 11172-11172/com.joshbgold.recommendations I/Process﹕ Sending signal. PID: 11172 SIG: 9
答案 0 :(得分:1)
您没有发布Api
类型定义的源代码(.create(Api.class);
引用的类型)。我从错误消息中猜测它被定义为public class Api { ... }
。它应该定义为public interface Api { ... }
。