1
2
3
4
|
interface SimpleGET{ @GET ( "/" ) //表明是GET方式.
"/"會拼接在setEndpoint(url)中url(主機地址)的後面. Response
getResponse(); //可以簡單的理解爲網絡訪問完把Response轉換爲一個對象.這裏沒有轉換,還是Response. } |
1
2
3
4
5
6
7
8
9
10
|
RestAdapter
adapter= new RestAdapter.Builder().setEndpoint(url).build(); //setEndpoint(url)設置網絡訪問地址的主機地址 SimpleGET
create = adapter.create(SimpleGET. class ); //retrofit會生成上面的SimpleGET接口的一個的實例. Response
response = create.getResponse(); int status
= response.getStatus(); //返回碼 try { InputStream
in = response.getBody().in(); //response的輸入流.看到網絡訪問的輸入流都很熟悉吧.想轉換成字符串還是json還是圖片就看服務器給的是什麼了. } catch (IOException
e) { e.printStackTrace(); } |
1
2
3
4
|
interface SimplePOST{ @POST ( "/android" ) //表明是使用Post方式進行訪問.
並且會把後面的路徑拼接到主機名的後面-->拼接到setEndpoint(url)中的url的後面. Response
getResponse(); } |
1
2
3
4
5
6
7
8
9
10
|
RestAdapter
adapter= new RestAdapter.Builder().setEndpoint(url).build(); //和上面post後面的路徑拼接,想當於訪問http://tieba.baidu.com/android SimplePOST
create = adapter.create(SimplePOST. class ); Response
response = create.getResponse(); int status
= response.getStatus(); try { InputStream
in = response.getBody().in(); } catch (IOException
e) { e.printStackTrace(); } |
1
2
3
|
interface GitHub
{ @GET ( "/repos/{owner}/{repo}/contributors" ) List<Contributor>
contributors( @Path ( "owner" )
String owner, @Path ( "repo" )
String repo); |
1
2
3
4
|
RestAdapter
restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).build(); GitHub
github = restAdapter.create(GitHub. class ); List<Contributor>
contributors = github.contributors( "square" , "retrofit" ); //相當於對路徑中{}部分進行動態替換.
相當於訪問https://api.github.com/repos/square/retrofit/contributors |
1
2
3
4
5
6
7
8
9
|
static class Contributor
{ //一個pojo類(Plain
Ordinary Java Object)簡單的Java對象-->相比javaBean更簡單. String
login; int contributions; } interface GitHub
{ @GET ( "/repos/{owner}/{repo}/contributors" ) List<Contributor>
contributors( @Path ( "owner" )
String owner, @Path ( "repo" )
String repo); } |
1
2
3
4
5
6
7
|
RestAdapter
restAdapter = new RestAdapter.Builder().setEndpoint(API_URL).build(); //默認設置了GsonConverter的轉換器.把response直接解析爲一個POJO對象. GitHub
github = restAdapter.create(GitHub. class ); //訪問這個地址返回的是一個JsonArray,JsonArray的每一個元素都有login和contributions這2個key和其對應的value.提取出來封裝進POJO對象中. List<Contributor>
contributors = github.contributors( "square" , "retrofit" ); for (Contributor
contributor : contributors) { Log.v( "retrofit" ,
contributor.login + "
(" +
contributor.contributions + ")" ); } |
JakeWharton (487)
pforhan (48)
edenman (40)
..........
[
{
"login": "JakeWharton",
"id": 66577,
............
"site_admin": false,
"contributions": 487
},
{
"login": "pforhan",
.............
"site_admin": false,
"contributions": 48
},
{
"login": "edenman",
"................
"site_admin": false,
"contributions": 40
},
...........
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class StringConverter implements Converter{ @Override public Object
fromBody(TypedInput input, Type type) throws ConversionException
{ //response輸入流到對象的轉換, try { InputStream
in = input.in(); //獲取了輸入流,就可以根據意願隨意轉換了. String
str = convertStream2String(in); return str; } catch (IOException
e) { e.printStackTrace(); } return null ; } private String
convertStream2String(InputStream in) throws IOException
{ //inputStream轉換爲String BufferedReader
reader= new BufferedReader( new InputStreamReader(in)); StringBuilder
sb= new StringBuilder(); String
line= null ; while ((line=reader.readLine())!= null ){ sb.append(line); sb.append( "\n" ); } return sb.toString(); } @Override public TypedOutput
toBody(Object obj) { //對象到輸出流的轉換 return null ; } } |
1
2
3
4
|
interface StringClient
{ @GET ( "/" ) String
getString(); //方法的返回值是String,需要StringConverter轉換器Converter把Response轉換爲String. } |
1
2
3
4
5
|
RestAdapter
adapter= new RestAdapter.Builder().setEndpoint(url).setConverter( new StringConverter()).build(); StringClient
create = adapter.create(StringClient. class ); String
str = create.getString(); |