让Retrofit直接返回你想要的结果
计划让Retrofit实现这样的效果。
public class Main {
public static void main(String[] args) {
GitHubService gitHubService = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(new DirectCallAdapterFactory())
.build()
.create(GitHubService.class);
// 通常情况retrofit的返回值需要是Call<User>类型
// 如果想要直接返回User,需要做些工作,参见下面的代码
User user = gitHubService.getUser("jijunpeng");
System.out.println(user);
}
}
接口详情参见页面github文档的相关说明 https://developer.github.com/v3/users/
-
GitHubService
定义github接口import retrofit2.http.GET; import retrofit2.http.Path; /** * 一般情况接口的返回值需要写成Call<User>的形式,但是我这里想直接获取User类,就需要做一些工作了 */ public interface GitHubService { /** * 获取github用户信息 */ @GET("/users/{username}") User getUser(@Path("username") String username); }
-
User
接受数据类import com.google.gson.annotations.SerializedName; import lombok.Data; /** * 从github接口创建出来的类。@Data是lombok的注解,我用来创建get和set方法 */ @Data public class User { private String login; private int id; @SerializedName("node_id") private String node_id; @SerializedName("avatar_url") private String avatar_url; @SerializedName("gravatar-id") private String gravatar_id; private String url; @SerializedName("html_url") private String htmlUrl; @SerializedName("followers_url") private String followersUrl; @SerializedName("following_url") private String followingUrl; @SerializedName("gists_url") private String gistsUrl; @SerializedName("starred_url") private String starredUrl; @SerializedName("subscriptions_url") private String subscriptionsUrl; @SerializedName("organizations_url") private String organizationsUrl; @SerializedName("repos_url") private String reposUrl; @SerializedName("events_url") private String eventsUrl; @SerializedName("received_events_url") private String receivedEventsUrl; private String type; @SerializedName("site_admin") private boolean siteAdmin; private String name; private String company; private String blog; private String location; private String email; private boolean hireable; private String bio; @SerializedName("public_repos") private int publicRepos; @SerializedName("public_gists") private int publicGists; private int followers; private int following; @SerializedName("created_at") private String createdAt; @SerializedName("updated_at") private String updatedAt; }
-
DirectCallAdapterFactory
实现可以直接返回数据类的核心类import retrofit2.Call; import retrofit2.CallAdapter; import retrofit2.Retrofit; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; /** * 实现可以直接返回数据类的核心类 */ public class DirectCallAdapterFactory extends CallAdapter.Factory { @Override public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { final Type responseType = getResponseType(returnType); return new CallAdapter<Object, Object>() { public Type responseType() { return responseType; } public Object adapt(Call<Object> call) { // todo 可以在这里判断接口数据格式 try { return call.execute().body(); } catch (IOException e) { e.printStackTrace(); } return null; } }; } private Type getResponseType(Type type) { if (type instanceof WildcardType) { return ((WildcardType) type).getUpperBounds()[0]; } return type; } }
直接执行 main
方法可以输出一下结果:
User(login=jijunpeng, id=8679345, node_id=MDQ6VXNlcjg2NzkzNDU=, avatar_url=https://avatars0.githubusercontent.com/u/8679345?v=4, gravatar_id=null, url=https://api.github.com/users/jijunpeng, htmlUrl=https://github.com/jijunpeng, followersUrl=https://api.github.com/users/jijunpeng/followers, followingUrl=https://api.github.com/users/jijunpeng/following{/other_user}, gistsUrl=https://api.github.com/users/jijunpeng/gists{/gist_id}, starredUrl=https://api.github.com/users/jijunpeng/starred{/owner}{/repo}, subscriptionsUrl=https://api.github.com/users/jijunpeng/subscriptions, organizationsUrl=https://api.github.com/users/jijunpeng/orgs, reposUrl=https://api.github.com/users/jijunpeng/repos, eventsUrl=https://api.github.com/users/jijunpeng/events{/privacy}, receivedEventsUrl=https://api.github.com/users/jijunpeng/received_events, type=User, siteAdmin=false, name=null, company=null, blog=, location=null, email=null, hireable=false, bio=null, publicRepos=14, publicGists=0, followers=0, following=1, createdAt=2014-09-06T16:07:44Z, updatedAt=2018-12-08T11:07:50Z)