在github上有一个retrofit的例子,获取论坛的信息
public class ForumService { private static final String FORUM_SERVER_URL = "http://jsonplaceholder.typicode.com"; private ForumApi mForumApi; public ForumService() { RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Accept", "application/json"); } }; RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(FORUM_SERVER_URL) .setRequestInterceptor(requestInterceptor) .setLogLevel(RestAdapter.LogLevel.FULL) .build(); //创建一个接口实例 mForumApi = restAdapter.create(ForumApi.class); } public ForumApi getApi() { return mForumApi; } public interface ForumApi { @GET("/posts") public Observable<List<Post>> getPosts(); @GET("/posts/{id}") public Observable<Post> getPost(@Path("id") int postId); @GET("/comments") public Observable<List<Comment>> getComments(@Query("postId") int postId); @POST("/posts") public Observable<Post> postPost(Post post); }} 代码中使用DetailPresenter 和ListPresenter程序运行时显示列表:
时间: 2024-10-07 05:16:23