Retrofit学习(二)集成-简单get请求

Retrofit学习(一)官网原文翻译
http://www.jianshu.com/writer#/notebooks/8054215/notes/9906248

· 在studio在添加依赖

compile 'com.squareup.retrofit2:retrofit:2.1.0'
//添加retrofit gson转换会自动下载gson
compile 'com.squareup.retrofit2:converter-gson:2.1.0'```

##·这是一个Get请求 能看到裸奔据说都一双慧眼
###a)网络权限 你能被坑的很惨

###b)建立接口

public interface GItHubService{
@GET("user/{user}/repos")
Call listRepos(@Path("user")String user);
}

[解析下:](http://www.goole.com/)
```@GET表示为get请求,还会有@POST
@PATH 表示后面的参数要添加到@GET后面对应的{user}中,{user}相当于一个占位符
@Query就是我们的请求的键值对的设置
@QueryMap 和@Query相似 就是个传个map集合,也是键值对```

 
###c)返回结果

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 > ListRepos(@Path("user") String user);
}

###f)再次请求
   //建立retrofit对象
  Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
          //添加返回字符串的支持--不知道返回的是什么,添加字符串支持
          .addConverterFactory(ScalarsConverterFactory.create())
          //添加GSON转换支持
          .addConverterFactory(GsonConverterFactory.create())
          .build();

  //获取接口
  GitHubService service = retrofit.create(GitHubService.class);

  //调用方法-返回 回调更换为对象
  Call> call = service.listRepos("octocat");

  //异步调用
  call.enqueue(new Callback>() {
      @Override
      public void onResponse(Call> call, Response> response) {

          L.d("vivi",response.message()+"  "+response.body());
          mTvResult.setText(response.message()+" \n结果: "+response.body().toString());
          Toast.makeText(FirstActivity.this, "结果:\n "+response.body().toString(), Toast.LENGTH_SHORT).show();
      }

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

          t.printStackTrace();
          mTvResult.setText(t.getMessage());

      }
  });

}

###g)简单封装

未完待续

###h)使用

未完待续

你可能感兴趣的:(Retrofit学习(二)集成-简单get请求)