一、Retrofit写一个网络请求:
1.引入Retrofit的包,在build.gradle文件中添加如下配置:
compile 'com.squareup.retrofit2:retrofit:2.1.0'//导入retrofit compile 'com.squareup.retrofit2:converter-gson:2.1.0' //转换器,请求结果转换成Model compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'io.reactivex:rxjava:1.2.1' compile 'com.google.code.gson:gson:2.6.2'//Gson 库 本人项目似乎没有使用这个 compile 'io.reactivex:rxandroid:1.2.1'//配合Rxjava 使用
2.创建一个Retrofit 实例,并且完成相关的配置:
配置了接口的 BASE_URL 和一个 converter , GsonConverterFactory 是默认提供的 Gson转换器。
在Application或者MultiDexApplication中
public class BaseApplication extends MultiDexApplication{
.................
/** * Retrofit配置信息 */ public Retrofit initRetrofit(String httpUrl) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS)//设置全局请求的连接超时时间,默认为15s .readTimeout(15, TimeUnit.SECONDS)//设置全局请求的数据读取超时时间,默认为30s .build(); //Retrofit的配置 Retrofit retrofit = new Retrofit.Builder() .baseUrl(httpUrl)//设置基础域名。网络请求由此拼接 .addConverterFactory(GsonConverterFactory.create())//设置json返回消息的解析库(这里使用gson) .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//此在与RxJava联用时才使用 .client(client)//配置okhttp配置。可无(为空时,retrofit会使用默认配置) .build(); //初始化请求地址api return retrofit; }
}
3.创建一个接口:
定义了一个方法 getTop250 ,使用 get请求方式,加上@GET 标签,标签后面是这个接口的 尾址top250,完整的地址应该是 baseUrl+尾址 ,参数 使用@Query标签,如果参数多的话可以用@QueryMap标 签,接收一个Map。
使用 POST 请求方式时,只需要更改方法定义的标签,用 @POST 标签,参数标签用 @Field 或者 @Body 或者 FieldMap
有了可以获取接口实例的方法,然后创建一个接口,代码如下:
public interface MovieService{
//获取豆瓣Top250 榜单
@GET("top250")
Observable<MovieSubject> getTop250(@Query("start") int start, @Query("count") int count);
@FormUrlEncoded
@POST("/x3/weather")
Call<String> getWeather(@Field("cityId") String cityId, @Field("key") String key);
}
我这边参考的代码,定义回调的接口 public interface ApiService {
//在线课程列表 @FormUrlEncoded @POST("courseAction!course.action") Observable<RxResult<LiveOnlineResult>> getLiveOnlineList(@Field("projectId") int projectId, @Field("examTypeId") int examTypeId);
}
以上代码返回类型 RxResult<LiveOnlineResult>
序列化LiveOnlineResult
public class RxResult<T> implements Serializable { /** * body : {"mobile":"18607917251","password":"170500"} * errorCode : -1 * msg : 获取验证码成功! * success : true */ private T body; private String errorCode; private String msg; private boolean success; public T getBody() { return body; } public void setBody(T body) { this.body = body; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } @Override public String toString() { return "RxResult{" + "body=" + body + ", errorCode='" + errorCode + '\'' + ", msg='" + msg + '\'' + ", success=" + success + '}'; } }
方法getLiveOnlineList()有两个参数projectId examTypeId
在函数中获取接口的回调参数
APP.apiService.getLiveOnlineList(1, examTypeId) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new RxSubscribe<LiveOnlineResult>() { @Override protected void _onNext(LiveOnlineResult liveOnlineResult) { mView.showOnline(liveOnlineResult.getSubjectList()); } @Override protected void _onError(String msg) { mView.showToast(msg); } });