Retrofit學(xué)習(xí)(一)集成-簡單get請求
Retrofit學(xué)習(xí)(一)-集成
http://www.itdecent.cn/p/86e5cddcc753
Retrofit學(xué)習(xí)(二)-get請求
http://www.itdecent.cn/p/cdde02a0777c
Retrofit學(xué)習(xí)(三)-普通post請求
http://www.itdecent.cn/p/7f252d10fd41
Retrofit學(xué)習(xí)(四)-下載文件
http://www.itdecent.cn/p/f61645a770ae
Retrofit學(xué)習(xí)(五)-文件上傳
http://www.itdecent.cn/p/ca0cb8640c8f
集成
githubhttps://github.com/square/retrofit
在studio在添加依賴
//添加retrofit-會自動下載okhttp
compile 'com.squareup.retrofit2:retrofit:2.1.0'
//添加retrofit gson轉(zhuǎn)換會自動下載gson
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//添加返回的字符串支付
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
可以用其它的retrofit2支付的替代見連接
轉(zhuǎn)換可以在API處找到
http://square.github.io/retrofit/#api-declaration
右側(cè)點(diǎn)擊Retrofit Configuration版本與retrofit2保持一直即可
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
第一個get請求
添加網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
建立接口
public interface GitHubService {
@GET("users/{user}/repos")
Call<String> listRepos(@Path("user") String user);
}
簡單說明一下
@GET表示為get請求,還會有@POST
@PATH 表示后面的參數(shù)要添加到@GET后面對應(yīng)的{user}中,{user}相當(dāng)于一個占位符
學(xué)習(xí)spring MVC都知道這種做學(xué)法把請求參數(shù)添加到請求路徑中去,
@Query就是我們的請求的鍵值對的設(shè)置
@QueryMap 和@Query相似 就是個傳個map集合,也是鍵值對
開始請求
界面就一個按鍵點(diǎn)擊可以請求
結(jié)果
OK [{"id":18221276,"name":"git-consortium","full_name":"octocat/git-consortium","owner":{"login":"octocat","id":583231,"avatar_url":"https://avatars.githubusercontent.com/u/583231?v=3","gravatar_id":"","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/git-consortium","description":"This repo is for demonstration purposes only.","fork":false,"url":"https://api.github.com/repos/octocat/git-consortium","forks_url":"https://api.github.com/repos/octocat/git-consortium/forks","keys_url":"https://api.github.com/repos/octocat/git-consortium/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/git-consortium/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/git-consortium/teams","hooks_url":"https://api.github.com/repos/octocat/git-consortium/hooks","issue_events_url":"https://api.github.com/repos/octocat/git-consortium/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/git-consortium/events","assignees_url":"https://api.github.com/repos/octocat/git-consortium/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/git-consortium/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/git-consortium/tags","blobs_url":"https://api.github.com/repos/octocat/git-consortium/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/git-consortium/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/git-consortium/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/git-consortium/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/git-consortium/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/git-consortium/languages","stargazers_url":"https://api.github.com/repos/octocat/git-consortium/stargazers","contributors_url":"https://api.github.com/repos/octocat/git-consortium/contributors","subscribers_url":"https://api.github.com/repos/octocat/git-consortium/subscribers","subscription_url":"https://api.github.com/repos/octocat/git-consortium/subscription","commits_url":"https://api.github.com/repos/octocat/git-consortium/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/git-consortium/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/git-consortium/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/git-consortium/issues/comments{/number}","contents_url":"https://api.github.com/repos/octocat/git-consortium/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/git-consortium/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/git-consortium/merges","archive_url":"https://api.github.com/repos/octocat/git-consortium/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/git-consortium/downloads","issues_url":"https://api.github.com/repos/octocat/git-consortium/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/git-consortium/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/git-consortium/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/git-consortium/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/git-consortium/labels{/name}","releases_url":"https://api.github.com
這樣一個網(wǎng)絡(luò)請求就完成了
把返回字符串返回實(shí)體類使用Gson
public class Repo {
public int id;
public String name;
public String full_name;
public OwnerEntity owner;
@SerializedName("private")
public boolean privateX;
public String html_url;
public Object description;
public boolean fork;
public String url;
public String forks_url;
public String keys_url;
public String collaborators_url;
public String teams_url;
public String hooks_url;
public String issue_events_url;
public String events_url;
public String assignees_url;
public String branches_url;
public String tags_url;
public String blobs_url;
public String git_tags_url;
public String git_refs_url;
public String trees_url;
public String statuses_url;
public String languages_url;
public String stargazers_url;
public String contributors_url;
public String subscribers_url;
public String subscription_url;
public String commits_url;
public String git_commits_url;
public String comments_url;
public String issue_comment_url;
public String contents_url;
public String compare_url;
public String merges_url;
public String archive_url;
public String downloads_url;
public String issues_url;
public String pulls_url;
public String milestones_url;
public String notifications_url;
public String labels_url;
public String releases_url;
public String deployments_url;
public String created_at;
public String updated_at;
public String pushed_at;
public String git_url;
public String ssh_url;
public String clone_url;
public String svn_url;
public Object homepage;
public int size;
public int stargazers_count;
public int watchers_count;
public String language;
public boolean has_issues;
public boolean has_downloads;
public boolean has_wiki;
public boolean has_pages;
public int forks_count;
public Object mirror_url;
public int open_issues_count;
public int forks;
public int open_issues;
public int watchers;
public String default_branch;
public static class OwnerEntity {
public String login;
public int id;
public String avatar_url;
public String gravatar_id;
public String url;
public String html_url;
public String followers_url;
public String following_url;
public String gists_url;
public String starred_url;
public String subscriptions_url;
public String organizations_url;
public String repos_url;
public String events_url;
public String received_events_url;
public String type;
public boolean site_admin;
}
}
記得添加toString方法方法查看
- 更改接口返回值
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
- 再次請求
//建立retrofit對象
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
//添加返回字符串的支持--不知道返回的是什么,添加字符串支持
.addConverterFactory(ScalarsConverterFactory.create())
//添加GSON轉(zhuǎn)換支持
.addConverterFactory(GsonConverterFactory.create())
.build();
//獲取接口
GitHubService service = retrofit.create(GitHubService.class);
//調(diào)用方法-返回 回調(diào)更換為對象
Call<List<Repo>> call = service.listRepos("octocat");
//異步調(diào)用
call.enqueue(new Callback<List<Repo>>() {
@Override
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
L.d("vivi",response.message()+" "+response.body());
mTvResult.setText(response.message()+" \n結(jié)果: "+response.body().toString());
Toast.makeText(FirstActivity.this, "結(jié)果:\n "+response.body().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<Repo>> call, Throwable t) {
t.printStackTrace();
mTvResult.setText(t.getMessage());
}
});
}
- 簡單封裝
public class RetrofitWrapper {
//單例
private static RetrofitWrapper INSTANCE;
// Retrofit 對象
private Retrofit mRetrofit;
private RetrofitWrapper(){
mRetrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL) //添加baseurl
.addConverterFactory(ScalarsConverterFactory.create()) //添加返回為字符串的支持
.addConverterFactory(GsonConverterFactory.create()) //create中可以傳入其它json對象,默認(rèn)Gson
.build();
}
public static RetrofitWrapper getInstance() {
if(INSTANCE == null) {
synchronized(RetrofitWrapper.class) {
if(INSTANCE == null) {
INSTANCE = new RetrofitWrapper();
}
}
}
return INSTANCE;
}
/**
* 轉(zhuǎn)換為對象的Service
* @param service
* @param <T>
* @return 傳入的類型
*/
public <T> T create(Class<T> service){
return mRetrofit.create(service);
}
}
- 使用
Call<List<Repo>> call = RetrofitWrapper.getInstance().create(GitHubService.class).listRepos("octocat");
call.enqueue(new Callback<List<Repo>>() {
@Override
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
// ...do something
}
@Override
public void onFailure(Call<List<Repo>> call, Throwable t) {
// ...do something
}
});
這樣用著方便很多
源碼:
https://github.com/ln0491/RetrofitDemo2