首页 > 代码库 > retrofit 用法

retrofit 用法

在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
程序运行时显示列表:

技术分享

 

retrofit 用法