Retrofit學(xué)習(xí)(一)官網(wǎng)原文翻譯
http://www.reibang.com/writer#/notebooks/8054215/notes/9906248
· 在studio在添加依賴
compile 'com.squareup.retrofit2:retrofit:2.1.0'
//添加retrofit gson轉(zhuǎn)換會自動下載gson
compile 'com.squareup.retrofit2:converter-gson:2.1.0'```
##·這是一個Get請求 能看到裸奔據(jù)說都一雙慧眼
###a)網(wǎng)絡(luò)權(quán)限 你能被坑的很慘
<uses-permission android:name="android.permission.INTERNET"/>
###b)建立接口
public interface GItHubService{
@GET("user/{user}/repos")
Call<String> listRepos(@Path("user")String user);
}
[解析下:](http://www.goole.com/)
```@GET表示為get請求,還會有@POST
@PATH 表示后面的參數(shù)要添加到@GET后面對應(yīng)的{user}中,{user}相當(dāng)于一個占位符
@Query就是我們的請求的鍵值對的設(shè)置
@QueryMap 和@Query相似 就是個傳個map集合,也是鍵值對```
###c)返回結(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
###d)Bean對象
利用GsonFromat 工具生成Bean對象
###e)更改接口返回值
public interface GitHubService{
@GET("user/{user}/repos")
Call <List<Repo>> ListRepos(@Path("user") String user);
}
###f)再次請求
//建立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());
}
});
}
###g)簡單封裝
未完待續(xù)
###h)使用
未完待續(xù)