Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Retrofit 入門學習,retrofit入門學習

Retrofit 入門學習,retrofit入門學習

編輯:關於android開發

Retrofit 入門學習,retrofit入門學習


Retrofit 入門學習官方RetrofitAPI

官方的一個例子

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

 

這些注解都有一個參數 value,用來配置其路徑,比如示例中的 users/{user}/repos, 我們還注意到在構造 Retrofit 之時我們還傳入了一個 baseUrl("https://api.github.com/"), 請求的完整 Url

https://api.github.com/users/{user}/repos 傳入user參數替換{user}

 

就是通過 baseUrl 與注解的 value(下面稱 “path“ ) 整合起來的,具體整合的規則如下:

  path 是絕對路徑的形式:
    path = "/apath",baseUrl = "http://host:port/a/b"
      Url = "http://host:port/apath"
    path 是相對路徑,baseUrl 是目錄形式:
      path = "apath",baseUrl = "http://host:port/a/b/"
      Url = "http://host:port/a/b/apath"
    path 是相對路徑,baseUrl 是文件形式:
      path = "apath",baseUrl = "http://host:port/a/b"
      Url = "http://host:port/a/apath"
    path 是完整的 Url:
      path = "http://host:port/aa/apath",baseUrl = "http://host:port/a/b"
      Url = "http://host:port/aa/apath"

 

(@Path("user") String user) 中user參數會替換 @GET("users/{user}/repos")中的{user}

建議采用第二種方式來配置,並盡量使用同一種路徑形式

請求

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

 

調用

Call<List<Repo>> repos = service.listRepos("octocat");

 

這個是個什麼鬼List 找了半天沒找到Repo這個類

嘗試去訪問

https://api.github.com/users/octocat/repos

 

終於有東東了,把結果轉成JAVA BEAN

 package com.liu.retrofit2demo.bean;

    import com.google.gson.annotations.SerializedName;

    /**
     * @Description: 描述
     * @AUTHOR 劉楠  Create By 2016/9/19 0019 18:10
     */
    public class Repo {


        /**
         * id : 68529654
         * name : aidlDemo
         * full_name : ln0491/aidlDemo
         * owner : {"login":"ln0491","id":10574995,"avatar_url":"https://avatars.githubusercontent.com/u/10574995?v=3","gravatar_id":"","url":"https://api.github.com/users/ln0491","html_url":"https://github.com/ln0491","followers_url":"https://api.github.com/users/ln0491/followers","following_url":"https://api.github.com/users/ln0491/following{/other_user}","gists_url":"https://api.github.com/users/ln0491/gists{/gist_id}","starred_url":"https://api.github.com/users/ln0491/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ln0491/subscriptions","organizations_url":"https://api.github.com/users/ln0491/orgs","repos_url":"https://api.github.com/users/ln0491/repos","events_url":"https://api.github.com/users/ln0491/events{/privacy}","received_events_url":"https://api.github.com/users/ln0491/received_events","type":"User","site_admin":false}
         * private : false
         * html_url : https://github.com/ln0491/aidlDemo
         * description : null
         * fork : false
         * url : https://api.github.com/repos/ln0491/aidlDemo
         * forks_url : https://api.github.com/repos/ln0491/aidlDemo/forks
         * keys_url : https://api.github.com/repos/ln0491/aidlDemo/keys{/key_id}
         * collaborators_url : https://api.github.com/repos/ln0491/aidlDemo/collaborators{/collaborator}
         * teams_url : https://api.github.com/repos/ln0491/aidlDemo/teams
         * hooks_url : https://api.github.com/repos/ln0491/aidlDemo/hooks
         * issue_events_url : https://api.github.com/repos/ln0491/aidlDemo/issues/events{/number}
         * events_url : https://api.github.com/repos/ln0491/aidlDemo/events
         * assignees_url : https://api.github.com/repos/ln0491/aidlDemo/assignees{/user}
         * branches_url : https://api.github.com/repos/ln0491/aidlDemo/branches{/branch}
         * tags_url : https://api.github.com/repos/ln0491/aidlDemo/tags
         * blobs_url : https://api.github.com/repos/ln0491/aidlDemo/git/blobs{/sha}
         * git_tags_url : https://api.github.com/repos/ln0491/aidlDemo/git/tags{/sha}
         * git_refs_url : https://api.github.com/repos/ln0491/aidlDemo/git/refs{/sha}
         * trees_url : https://api.github.com/repos/ln0491/aidlDemo/git/trees{/sha}
         * statuses_url : https://api.github.com/repos/ln0491/aidlDemo/statuses/{sha}
         * languages_url : https://api.github.com/repos/ln0491/aidlDemo/languages
         * stargazers_url : https://api.github.com/repos/ln0491/aidlDemo/stargazers
         * contributors_url : https://api.github.com/repos/ln0491/aidlDemo/contributors
         * subscribers_url : https://api.github.com/repos/ln0491/aidlDemo/subscribers
         * subscription_url : https://api.github.com/repos/ln0491/aidlDemo/subscription
         * commits_url : https://api.github.com/repos/ln0491/aidlDemo/commits{/sha}
         * git_commits_url : https://api.github.com/repos/ln0491/aidlDemo/git/commits{/sha}
         * comments_url : https://api.github.com/repos/ln0491/aidlDemo/comments{/number}
         * issue_comment_url : https://api.github.com/repos/ln0491/aidlDemo/issues/comments{/number}
         * contents_url : https://api.github.com/repos/ln0491/aidlDemo/contents/{+path}
         * compare_url : https://api.github.com/repos/ln0491/aidlDemo/compare/{base}...{head}
         * merges_url : https://api.github.com/repos/ln0491/aidlDemo/merges
         * archive_url : https://api.github.com/repos/ln0491/aidlDemo/{archive_format}{/ref}
         * downloads_url : https://api.github.com/repos/ln0491/aidlDemo/downloads
         * issues_url : https://api.github.com/repos/ln0491/aidlDemo/issues{/number}
         * pulls_url : https://api.github.com/repos/ln0491/aidlDemo/pulls{/number}
         * milestones_url : https://api.github.com/repos/ln0491/aidlDemo/milestones{/number}
         * notifications_url : https://api.github.com/repos/ln0491/aidlDemo/notifications{?since,all,participating}
         * labels_url : https://api.github.com/repos/ln0491/aidlDemo/labels{/name}
         * releases_url : https://api.github.com/repos/ln0491/aidlDemo/releases{/id}
         * deployments_url : https://api.github.com/repos/ln0491/aidlDemo/deployments
         * created_at : 2016-09-18T15:24:32Z
         * updated_at : 2016-09-18T15:29:01Z
         * pushed_at : 2016-09-19T06:06:09Z
         * git_url : git://github.com/ln0491/aidlDemo.git
         * ssh_url : [email protected]:ln0491/aidlDemo.git
         * clone_url : https://github.com/ln0491/aidlDemo.git
         * svn_url : https://github.com/ln0491/aidlDemo
         * homepage : null
         * size : 229
         * stargazers_count : 0
         * watchers_count : 0
         * language : Java
         * has_issues : true
         * has_downloads : true
         * has_wiki : true
         * has_pages : false
         * forks_count : 0
         * mirror_url : null
         * open_issues_count : 0
         * forks : 0
         * open_issues : 0
         * watchers : 0
         * default_branch : master
         */

        public int id;
        public String name;
        public String full_name;
        /**
         * login : ln0491
         * id : 10574995
         * avatar_url : https://avatars.githubusercontent.com/u/10574995?v=3
         * gravatar_id :
         * url : https://api.github.com/users/ln0491
         * html_url : https://github.com/ln0491
         * followers_url : https://api.github.com/users/ln0491/followers
         * following_url : https://api.github.com/users/ln0491/following{/other_user}
         * gists_url : https://api.github.com/users/ln0491/gists{/gist_id}
         * starred_url : https://api.github.com/users/ln0491/starred{/owner}{/repo}
         * subscriptions_url : https://api.github.com/users/ln0491/subscriptions
         * organizations_url : https://api.github.com/users/ln0491/orgs
         * repos_url : https://api.github.com/users/ln0491/repos
         * events_url : https://api.github.com/users/ln0491/events{/privacy}
         * received_events_url : https://api.github.com/users/ln0491/received_events
         * type : User
         * site_admin : false
         */

        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;

            @Override
            public String toString() {
                return "OwnerEntity{" +
                        "login='" + login + '\'' +
                        ", id=" + id +
                        ", avatar_url='" + avatar_url + '\'' +
                        ", gravatar_id='" + gravatar_id + '\'' +
                        ", url='" + url + '\'' +
                        ", html_url='" + html_url + '\'' +
                        ", followers_url='" + followers_url + '\'' +
                        ", following_url='" + following_url + '\'' +
                        ", gists_url='" + gists_url + '\'' +
                        ", starred_url='" + starred_url + '\'' +
                        ", subscriptions_url='" + subscriptions_url + '\'' +
                        ", organizations_url='" + organizations_url + '\'' +
                        ", repos_url='" + repos_url + '\'' +
                        ", events_url='" + events_url + '\'' +
                        ", received_events_url='" + received_events_url + '\'' +
                        ", type='" + type + '\'' +
                        ", site_admin=" + site_admin +
                        '}';
            }
        }

        @Override
        public String toString() {
            return "Repo{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", full_name='" + full_name + '\'' +
                    ", owner=" + owner +
                    ", privateX=" + privateX +
                    ", html_url='" + html_url + '\'' +
                    ", description=" + description +
                    ", fork=" + fork +
                    ", url='" + url + '\'' +
                    ", forks_url='" + forks_url + '\'' +
                    ", keys_url='" + keys_url + '\'' +
                    ", collaborators_url='" + collaborators_url + '\'' +
                    ", teams_url='" + teams_url + '\'' +
                    ", hooks_url='" + hooks_url + '\'' +
                    ", issue_events_url='" + issue_events_url + '\'' +
                    ", events_url='" + events_url + '\'' +
                    ", assignees_url='" + assignees_url + '\'' +
                    ", branches_url='" + branches_url + '\'' +
                    ", tags_url='" + tags_url + '\'' +
                    ", blobs_url='" + blobs_url + '\'' +
                    ", git_tags_url='" + git_tags_url + '\'' +
                    ", git_refs_url='" + git_refs_url + '\'' +
                    ", trees_url='" + trees_url + '\'' +
                    ", statuses_url='" + statuses_url + '\'' +
                    ", languages_url='" + languages_url + '\'' +
                    ", stargazers_url='" + stargazers_url + '\'' +
                    ", contributors_url='" + contributors_url + '\'' +
                    ", subscribers_url='" + subscribers_url + '\'' +
                    ", subscription_url='" + subscription_url + '\'' +
                    ", commits_url='" + commits_url + '\'' +
                    ", git_commits_url='" + git_commits_url + '\'' +
                    ", comments_url='" + comments_url + '\'' +
                    ", issue_comment_url='" + issue_comment_url + '\'' +
                    ", contents_url='" + contents_url + '\'' +
                    ", compare_url='" + compare_url + '\'' +
                    ", merges_url='" + merges_url + '\'' +
                    ", archive_url='" + archive_url + '\'' +
                    ", downloads_url='" + downloads_url + '\'' +
                    ", issues_url='" + issues_url + '\'' +
                    ", pulls_url='" + pulls_url + '\'' +
                    ", milestones_url='" + milestones_url + '\'' +
                    ", notifications_url='" + notifications_url + '\'' +
                    ", labels_url='" + labels_url + '\'' +
                    ", releases_url='" + releases_url + '\'' +
                    ", deployments_url='" + deployments_url + '\'' +
                    ", created_at='" + created_at + '\'' +
                    ", updated_at='" + updated_at + '\'' +
                    ", pushed_at='" + pushed_at + '\'' +
                    ", git_url='" + git_url + '\'' +
                    ", ssh_url='" + ssh_url + '\'' +
                    ", clone_url='" + clone_url + '\'' +
                    ", svn_url='" + svn_url + '\'' +
                    ", homepage=" + homepage +
                    ", size=" + size +
                    ", stargazers_count=" + stargazers_count +
                    ", watchers_count=" + watchers_count +
                    ", language='" + language + '\'' +
                    ", has_issues=" + has_issues +
                    ", has_downloads=" + has_downloads +
                    ", has_wiki=" + has_wiki +
                    ", has_pages=" + has_pages +
                    ", forks_count=" + forks_count +
                    ", mirror_url=" + mirror_url +
                    ", open_issues_count=" + open_issues_count +
                    ", forks=" + forks +
                    ", open_issues=" + open_issues +
                    ", watchers=" + watchers +
                    ", default_branch='" + default_branch + '\'' +
                    '}';
        }
    }

 

使用

導入依賴

     compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'

 

建立Service

public interface GitHubService {


    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);
}

 

構造 Retrofit 使用單例

public class RetrofitWrapper {

    private static RetrofitWrapper instance;
    private        Retrofit        mRetrofit;

    private RetrofitWrapper() {

        //初始化 添加轉換工廠
        mRetrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }


    public static RetrofitWrapper getInstance() {

        if(instance == null) {
            synchronized(RetrofitWrapper.class) {
                if(instance == null) {
                    instance = new RetrofitWrapper();
                }
            }
        }

        return instance;
    }

    /**
     * 轉換為對象的Service
     * @param service
     * @param <T>
     * @return
     */
    public <T> T create(Class<T> service){
        return mRetrofit.create(service);
    }

    /**
     * 常量類 基本的URL
     */
    public class Constant {
            //BASE_URL 可以自行替換
        public static final String BASE_URL = "https://api.github.com/";
    }
}

 

在Activity中調用

添加網絡權限

 <uses-permission android:name="android.permission.INTERNET"/>

 

在點擊事件時調用

private void getUserRepo() {

        GitHubService gitHubService = RetrofitWrapper.getInstance().create(GitHubService.class);

        Call<List<Repo>> call = gitHubService.listRepos("octocat");


        call.enqueue(new Callback<List<Repo>>() {
            @Override
            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                //接收結果
                List<Repo> list = response.body();

                Log.d(TAG, "onResponse: " + list.size() + "    " + list.toString());
                Log.d(TAG, "onResponse:當前線程 "+Thread.currentThread().getName());
                                 /**
                                 * 回調是在主線程可以更新UI
                                 */
                                mTv.setText(list.get(0).full_name);
                                mPb.setProgress(50);
            }

            @Override
            public void onFailure(Call<List<Repo>> call, Throwable t) {

                Log.d(TAG, "onFailure: " + t.getMessage());
                Log.d(TAG, "onFailure:當前線程 "+Thread.currentThread().getName());
            }
        });


    }

 

日志

onResponse: 7    [Repo{id=18221276, name='git-...太長省略
onResponse:當前線程 main

 

不容易,終於成功了. 突然覺得請求接口就好像訪問自家的方法 也看到了回調是在主線程中。可以做UI操作

接都走官方的API

BASE_URL

https://api.github.com/

 

API

http://square.github.io/retrofit/

 

public interface GitHubService {


    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);


    @GET("users/list")
    Call<User> getUser();
}

 

調用

private void getUser() {
        GitHubService gitHubService = RetrofitWrapper.getInstance().create(GitHubService.class);

        Call<User> userCall = gitHubService.getUser();

        userCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();

                Log.d(TAG, "getUser onResponse: "+user.toString());
                Log.d(TAG, " getUser onResponse: "+response.code()+"    "+response.isSuccessful()+"    "+response.message());
                mTv.setText(user.toString());
                mPb.setProgress(100);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }

 

日志

 getUser onResponse: User{login='list', id=1133421, avatar_url='https://avatars.githubusercontent.com/u/1133421?v=3',...省略
  getUser onResponse: 200    true    OK

 

訪問根路徑

@GET("/")
Call<GithubBean> getGitHubApi();

 

一定要有 ("/") 不然會報錯

private void getGithubApi() {

    GitHubService gitHubService = RetrofitWrapper.getInstance().create(GitHubService.class);
    Call<GithubBean> apiBeanCall = gitHubService.getGitHubApi();

    apiBeanCall.enqueue(new Callback<GithubBean>() {
        @Override
        public void onResponse(Call<GithubBean> call, Response<GithubBean> response) {

            Log.d(TAG, "getGithubApi onResponse: "+response.message());
            GithubBean githubBean = response.body();

            Log.d(TAG, "onResponse: "+githubBean.toString());
            mTv.setText(githubBean.toString());
            mPb.setProgress(78);
        }

        @Override
        public void onFailure(Call<GithubBean> call, Throwable t) {
            Log.d(TAG, "getGithubApi onFailure: "+t.getMessage());
        }
    });

}

 

帶參數的get請求

Query
public interface MyServcie {



@GET(AppUrl.GET_USE_HELP)
Call<UseHelpBean> getUseHelp(@Query("sign")String sign,@Query("appKey") String appKey,@Query("osName")String osName);

}

 

日志

getUseHelp onResponse: OK
onResponse: UseHelpBean{info=InfoEntity{questList=[QuestListEntity{typeName='生活寶箱', reply='可以,選擇你想加為好友的用戶,點擊“加好友”便可。', typeId=1002}, QuestListEntity{typeName='賬戶', reply='可以,點擊用戶...

 

QueryMap

@GET(AppUrl.GET_USE_HELP)
Call<UseHelpBean> getUseHelp(@Query("sign")String sign,@Query("appKey") String appKey,@Query("osName")int osName);


@GET(AppUrl.GET_USE_HELP)
Call<UseHelpBean> getUseHelps(@QueryMap Map<String,String> params);

 

結果與上面一樣

再看一個天氣的

https://www.juhe.cn/docs/api/id/235

 


開放的API可以自己申請

BASE_URL

http://v.juhe.cn/weather/
key:88ca9906a9ac5408ecd46b42c8e5324f

 

Service

public interface WeatherService {


    @GET("index")
    Call<WeatherData> getWeather(@Query("cityname") String cityname,@Query("key") String key,@Query("dtype") String dtype,@Query("format") int format);


    @GET("index")
    Call<WeatherData> getWeathers(@QueryMap Map<String,String> params);
}

 

POST請求 --POST 提交表單Field & FieldMap

表單-參數Field

@FormUrlEncoded
@POST("index")
Call<CheckBean> getInfo(@Field("info")String info,@Field("key")String key);

 

表單 -多參數用FieldMap

@FormUrlEncoded
@POST("index")
Call<CheckBean> getInfos(@FieldMap Map<String,String> params);

 

源碼:

https://github.com/ln0491/Retrofit2Demo

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved