Retrofit + RxJava这两个组合起来真的炒鸡好用,只是可能第一步比较难踏出去,刚开始不太理解这两个东西是什么,后来是看了这篇文章:
http://gank.io/post/560e15be2dca930e00da1083
原理就不介绍了,上面那个是很好的资料
一、首先要用到Retrofit和RxJava通常需要用到以下依赖
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.google.code.gson:gson:2.7'
另:因为这里还要处理json数据我用的是以下的库
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.22'
如果想查这些库的其他版本给个网址(以前居然不知道,一直是各种搜索。。)
二、因为需要连接网络,不要忘了配置网络权限
三、封装的代码
public interface HttpApi {
@FormUrlEncoded
@POST("{path}")
Observable<JSONObject> post(@Path("path") String path, @FieldMap Map<String, String> map);
@FormUrlEncoded
@POST("{root}/{path}")
Observable<JSONObject> post(@Path("root") String root, @Path("path") String path, @FieldMap Map<String, String> map);
@FormUrlEncoded
@POST("{root}/{path}")
Observable<JSONObject> post(@Path("root") String root, @Path("path") String path);
@FormUrlEncoded
@POST("{path}")
Observable<JSONObject> post(@Path("path") String path);
@GET("{path}")
Observable<JSONObject> get(@Path("path") String path);
@GET("{path}")
Observable<JSONObject> get(@Path("path") String path, @QueryMap Map<String, String> map);
}
public class HttpRequests {
private static String baseUrl = "http://127.0.0.1:8080";
private static HttpRequests instance = null;
private HttpApi httpService;
public static HttpRequests getInstance() {
if (instance == null) {
synchronized (HttpRequests.class) {
if (instance == null) {
instance = new HttpRequests();
}
}
}
return instance;
}
private HttpRequests() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
httpService = retrofit.create(HttpApi.class);
}
public Observable post(String path, Map<String, String> map) {
try {
Observable<JSONObject> observable;
if (path.split("/").length > 1) {
String root = path.split("/")[0];
path = path.split("/")[1];
if (map != null)
observable = httpService.post(root, path, map);
else
observable = httpService.post(root, path);
} else if (map != null)
observable = httpService.post(path, map);
else
observable = httpService.post(path);
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
return observable;
} catch (Exception e) {
Log.e("error", e.getMessage());
return null;
}
}
public Observable get(String path, Map<String, String> map) {
try {
Observable<JSONObject> observable;
if (map != null) {
observable = httpService.get(path, map);
} else {
observable = httpService.get(path);
}
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
return observable;
} catch (Exception e) {
Log.e("lawliex", e.getMessage());
return null;
}
}
}
四、 使用方法
//假设我们要请求的地址是http://localhost:8080/path
//map用来保存请求参数
//post方法的的第一个参数path是我们请求url的path
Map<String,String> map = new HashMap<>();
map.put("id","id");
map.put("ticket","ticket");
HttpRequests.getInstance().post("path",map)
.subscribe(new Subscriber<JSONObject>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(JSONObject jsonObject) {
//jsonObject就是我们获取到的json数据
//在这里可以做一些成功获取数据的操作
}
});