Retrofit学习简单实用

#使用Retrofit步骤

添加build.gradle

1
2
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0

##如何和请求数据

###GET方式

####首先定义一个接口 用于存放所有的请求地址
例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
定义一个接口叫HttpService 用来存放一些地址路径
public interface HttpService {
@GET("api/v2/getAliasName.jspx/userid={userid}")
Call<ResponseBody> getAliasName(@Path("userid") int userid);
@GET("api/v2/getAliasName.jspx")
Call<String> getAliasName2(@Query("userid") int userid);
@POST("api/v2/getAliasName.jspx")
@FormUrlEncoded
Call<ResponseBody> getAliasName3(@Field("userid") int userid);
@GET("/")
Call<String> getBaidu();
}

这样里面就有了你要请求的一些接口,@GET注解表示请求的是get方法
注解括号里面的 "api/v2/getAliasName.jspx/userid={userid}" 这是你要请求的路径,
就想我们正常写网络请求时的写法一样 一般写一个请求拼在一起
host+path
如:

1
2
3
host: public final static String host="http:// 220.181.112.244/jeecms";
path: public final static String getAliasName=host+"api/v2/getAliasName.jspx";
RequestParams paramsn = new RequestParams(HttpConstants.getAliasName);

Call后面返回的泛型就是,服务器返回数据的格式,@Path这个注解路径替换,会用传进来的userid 替换路径上的{userid};
@Query这个注解,会以key-valule的形式拼接在路径的后面
如 :/jeecms/api/v2/getAliasName.jspx?userid=4910
写一个GET请求

####1.先创建一个Retrofit

1
2
3
Retrofit retrofit=new Retrofit.Builder().
baseUrl("http://www.baidu.com/")
.build();

baseUrl就是你要请求的host.

####2.具体请求哪个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HttpService nameService=retrofit.create(HttpService.class);
Call<String> call=nameService.getBaidu();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
view.setText(response.body());
Log.e("ssss",response.toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("",t.toString());
}
});

请求了HttpService 中getBaidu()方法。这样就可以了?
刚开始以为这就行了,运行崩溃,会发现这个异常
java.lang.IllegalArgumentException: Unable to create converter for class java.lang.String
这是这个请求没有转换器,不能把返回来的数据转换成String 类型。
那么应该如何做?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Retrofit retrofit=new Retrofit.Builder().
baseUrl("http://www.baidu.com/")
.addConverterFactory(new Converter.Factory() {
@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
})
.build();

将1.中添加如上的转换器就可以了,return new Converter<ResponseBody, String>,泛型中的String就是要转换的类型,public String convert(ResponseBody value)方法中 将value转成String就可以了。

这样太麻烦,所以我们可以用 我们在build中添加的 这个库来转换compile 'com.squareup.retrofit2:converter-gson:2.3.0
.addConverterFactory(GsonConverterFactory.create())
一句话就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Retrofit retrofit=new Retrofit.Builder().
baseUrl("http://www.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
HttpService nameService=retrofit.create(HttpService.class);
Call<String> call=nameService.getBaidu();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
view.setText(response.body());
Log.e("ssss",response.toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("",t.toString());
}
});

这样是不是就舒服多了。

###POST方式
@POST(“api/v2/getAliasName.jspx”)
@FormUrlEncoded
Call getAliasName3(@Field(“userid”) int userid);
注解用@POST,和@FromUrlEncoed
@Field()来注解key,传进来的就是value。这并不会追加在路径后面。
如:
userid=4910
学习链接:
好用的网络请求库Retrofit2(入门及讲解)
你真的会用Retrofit2吗?Retrofit2完全教程