Retrofit學(xué)習(xí)(二)集成-簡單get請求

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ù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末盈简,一起剝皮案震驚了整個濱河市舶担,隨后出現(xiàn)的幾起案子萄焦,更是在濱河造成了極大的恐慌驾诈,老刑警劉巖毒姨,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哑蔫,死亡現(xiàn)場離奇詭異,居然都是意外死亡弧呐,警方通過查閱死者的電腦和手機闸迷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來俘枫,“玉大人腥沽,你說我怎么就攤上這事”懒ǎ” “怎么了巡球?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長邓嘹。 經(jīng)常有香客問我酣栈,道長,這世上最難降的妖魔是什么汹押? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任矿筝,我火速辦了婚禮,結(jié)果婚禮上棚贾,老公的妹妹穿的比我還像新娘窖维。我一直安慰自己榆综,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布铸史。 她就那樣靜靜地躺著鼻疮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪琳轿。 梳的紋絲不亂的頭發(fā)上判沟,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天,我揣著相機與錄音崭篡,去河邊找鬼挪哄。 笑死,一個胖子當(dāng)著我的面吹牛琉闪,可吹牛的內(nèi)容都是我干的迹炼。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼颠毙,長吁一口氣:“原來是場噩夢啊……” “哼斯入!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吟秩,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤咱扣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后涵防,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體闹伪,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年壮池,在試婚紗的時候發(fā)現(xiàn)自己被綠了偏瓤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡椰憋,死狀恐怖厅克,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情橙依,我是刑警寧澤证舟,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站窗骑,受9級特大地震影響女责,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜创译,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一抵知、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦刷喜、人聲如沸残制。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽初茶。三九已至,卻和暖如春浊闪,著一層夾襖步出監(jiān)牢的瞬間纺蛆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工规揪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人温峭。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓猛铅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凤藏。 傳聞我的和親對象是個殘疾皇子奸忽,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容