@作者 : 西野奈留
@博客:http://blog.csdn.net/narunishino
-2016/4/29-
1个activity,1个接口,1个实体。
// MainActivity.java
package com.nn.retrofitpractise;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import com.nn.retrofitpractise.pojo.Data;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
MyApiEndpointInterface myApiEndpointInterface;
TextView textView;
//https://api.github.com/users/basil2style
public static final String BASE_URL = "https://api.github.com";
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
exeRetrofit();
}
private void exeRetrofit() {
/**
* whats more?
* 1,需要一个实体,来装载数据。
* 2,需要一个interface,包含一个发起请求的连接和一个callback方法。
*/
//第一步,初始化Retrofit。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
//第二步,关联interface。
myApiEndpointInterface = retrofit.create(MyApiEndpointInterface.class);
//第三步,关联实体。
Call<Data> call = myApiEndpointInterface.getData();
//第四步,回调。
call.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
textView.setText(response.body().getLocation());
Log.v(TAG, "response= " + response.body().getLocation());
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
}
});
}
}
// MyApiEndpointInterface.java
package com.nn.retrofitpractise;
import com.nn.retrofitpractise.pojo.Data;
import retrofit2.Call;
import retrofit2.http.GET;
public interface MyApiEndpointInterface {
//https://api.github.com/users/basil2style
@GET("/users/basil2style")
Call<Data> getData();
}
// Data.java
package com.nn.retrofitpractise.pojo;
public class Data {
private String login;
private Integer id;
private String avatarUrl;
private String gravatarId;
private String url;
private String htmlUrl;
private String followersUrl;
private String followingUrl;
private String gistsUrl;
private String starredUrl;
private String subscriptionsUrl;
private String organizationsUrl;
private String reposUrl;
private String eventsUrl;
private String receivedEventsUrl;
private String type;
private Boolean siteAdmin;
private String name;
private String company;
private String blog;
private String location;
private String email;
private Boolean hireable;
private Object bio;
private Integer publicRepos;
private Integer publicGists;
private Integer followers;
private Integer following;
private String createdAt;
private String updatedAt;
/**
* @return The login
*/
public String getLogin() {
return login;
}
/**
* @param login The login
*/
public void setLogin(String login) {
this.login = login;
}
/**
* @return The id
*/
public Integer getId() {
return id;
}
/**
* @param id The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return The avatarUrl
*/
public String getAvatarUrl() {
return avatarUrl;
}
/**
* @param avatarUrl The avatar_url
*/
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
/**
* @return The gravatarId
*/
public String getGravatarId() {
return gravatarId;
}
/**
* @param gravatarId The gravatar_id
*/
public void setGravatarId(String gravatarId) {
this.gravatarId = gravatarId;
}
/**
* @return The url
*/
public String getUrl() {
return url;
}
/**
* @param url The url
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return The htmlUrl
*/
public String getHtmlUrl() {
return htmlUrl;
}
/**
* @param htmlUrl The html_url
*/
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
/**
* @return The followersUrl
*/
public String getFollowersUrl() {
return followersUrl;
}
/**
* @param followersUrl The followers_url
*/
public void setFollowersUrl(String followersUrl) {
this.followersUrl = followersUrl;
}
/**
* @return The followingUrl
*/
public String getFollowingUrl() {
return followingUrl;
}
/**
* @param followingUrl The following_url
*/
public void setFollowingUrl(String followingUrl) {
this.followingUrl = followingUrl;
}
/**
* @return The gistsUrl
*/
public String getGistsUrl() {
return gistsUrl;
}
/**
* @param gistsUrl The gists_url
*/
public void setGistsUrl(String gistsUrl) {
this.gistsUrl = gistsUrl;
}
/**
* @return The starredUrl
*/
public String getStarredUrl() {
return starredUrl;
}
/**
* @param starredUrl The starred_url
*/
public void setStarredUrl(String starredUrl) {
this.starredUrl = starredUrl;
}
/**
* @return The subscriptionsUrl
*/
public String getSubscriptionsUrl() {
return subscriptionsUrl;
}
/**
* @param subscriptionsUrl The subscriptions_url
*/
public void setSubscriptionsUrl(String subscriptionsUrl) {
this.subscriptionsUrl = subscriptionsUrl;
}
/**
* @return The organizationsUrl
*/
public String getOrganizationsUrl() {
return organizationsUrl;
}
/**
* @param organizationsUrl The organizations_url
*/
public void setOrganizationsUrl(String organizationsUrl) {
this.organizationsUrl = organizationsUrl;
}
/**
* @return The reposUrl
*/
public String getReposUrl() {
return reposUrl;
}
/**
* @param reposUrl The repos_url
*/
public void setReposUrl(String reposUrl) {
this.reposUrl = reposUrl;
}
/**
* @return The eventsUrl
*/
public String getEventsUrl() {
return eventsUrl;
}
/**
* @param eventsUrl The events_url
*/
public void setEventsUrl(String eventsUrl) {
this.eventsUrl = eventsUrl;
}
/**
* @return The receivedEventsUrl
*/
public String getReceivedEventsUrl() {
return receivedEventsUrl;
}
/**
* @param receivedEventsUrl The received_events_url
*/
public void setReceivedEventsUrl(String receivedEventsUrl) {
this.receivedEventsUrl = receivedEventsUrl;
}
/**
* @return The type
*/
public String getType() {
return type;
}
/**
* @param type The type
*/
public void setType(String type) {
this.type = type;
}
/**
* @return The siteAdmin
*/
public Boolean getSiteAdmin() {
return siteAdmin;
}
/**
* @param siteAdmin The site_admin
*/
public void setSiteAdmin(Boolean siteAdmin) {
this.siteAdmin = siteAdmin;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name The name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The company
*/
public String getCompany() {
return company;
}
/**
* @param company The company
*/
public void setCompany(String company) {
this.company = company;
}
/**
* @return The blog
*/
public String getBlog() {
return blog;
}
/**
* @param blog The blog
*/
public void setBlog(String blog) {
this.blog = blog;
}
/**
* @return The location
*/
public String getLocation() {
return location;
}
/**
* @param location The location
*/
public void setLocation(String location) {
this.location = location;
}
/**
* @return The email
*/
public String getEmail() {
return email;
}
/**
* @param email The email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return The hireable
*/
public Boolean getHireable() {
return hireable;
}
/**
* @param hireable The hireable
*/
public void setHireable(Boolean hireable) {
this.hireable = hireable;
}
/**
* @return The bio
*/
public Object getBio() {
return bio;
}
/**
* @param bio The bio
*/
public void setBio(Object bio) {
this.bio = bio;
}
/**
* @return The publicRepos
*/
public Integer getPublicRepos() {
return publicRepos;
}
/**
* @param publicRepos The public_repos
*/
public void setPublicRepos(Integer publicRepos) {
this.publicRepos = publicRepos;
}
/**
* @return The publicGists
*/
public Integer getPublicGists() {
return publicGists;
}
/**
* @param publicGists The public_gists
*/
public void setPublicGists(Integer publicGists) {
this.publicGists = publicGists;
}
/**
* @return The followers
*/
public Integer getFollowers() {
return followers;
}
/**
* @param followers The followers
*/
public void setFollowers(Integer followers) {
this.followers = followers;
}
/**
* @return The following
*/
public Integer getFollowing() {
return following;
}
/**
* @param following The following
*/
public void setFollowing(Integer following) {
this.following = following;
}
/**
* @return The createdAt
*/
public String getCreatedAt() {
return createdAt;
}
/**
* @param createdAt The created_at
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
/**
* @return The updatedAt
*/
public String getUpdatedAt() {
return updatedAt;
}
/**
* @param updatedAt The updated_at
*/
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
-End-