> 软件编程 > 安卓开发 >
Retrofit+RxJava,Http请求get和post方法获取json数据简单封装 2017-01-04 12:18 出处:未知 人气:
Retrofit + RxJava这两个组合起来真的炒鸡好用,只是可能第一步比较难踏出去,刚开始不太理解这两个东西是什么,后来是看了这篇文章:
原理就不介绍了,上面那个是很好的资料
一、首先要用到Retrofit和RxJava通常需要用到以下依赖
另:因为这里还要处理json数据我用的是以下的库
compile , 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 = UrlContract.SERVER_ADDRESS; private static HttpRequests instance = null; private Observable<JSONObject> observable; private Subscriber subscriber; 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 void post(String path, Map<String, String> map) { try { 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()) .subscribe(subscriber); }catch(Exception e){ Log.e("error",e.getMessage()); } } public void get(String path, Map<String,String> map){ try { if (map != null) { observable = httpService.get(path, map); } else { observable = httpService.get(path); } observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber); }catch (Exception e){ Log.e("lawliex",e.getMessage()); } } public HttpRequests baseUrl(String url){ baseUrl = url; return instance; } public HttpRequests subscribe(Subscriber<JSONObject> subscriber){ this.subscriber = subscriber; return instance; } }四、 使用方法
Map<String,String> map = new HashMap<>(); map.put("id","id"); map.put("ticket","ticket"); HttpRequests.getInstance().baseUrl(":8080").subscribe(new Subscriber<JSONObject>() { { } { } { //jsonObject就是我们获取到的json数据 //在这里可以做一些成功获取数据的操作 } }).post("path",map);分享给小伙伴们:
本文标签: Retrofit,RxJava/">Retrofit,RxJava
相关文章
发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。
本类最热新闻