首页 文章 精选 留言 我的

精选列表

搜索[多分片],共10000篇文章
优秀的个人博客,低调大师

retrofit网络请求入门(多实例讲解)

之前就想抽空写一篇关于retrofit的博客,今天终于有空啦= =,话不多说。上博。 一.相关资料: Github:http://github.com/square/retrofit 官网文档:http://square.github.io/retrofit/ 相关博客:Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava 这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解) 二.:模拟实现的请求以及retrofit实现步骤 本次主要使用retrofit模拟实现 1.正常get请求 2.拼接get请求(单个参数) 3.拼接get请求(多个参数) 4.get占位符请求 5.post请求 概览.JPG retrofit实现步骤: 步骤1:添加Retrofit库的依赖 步骤2:创建 接收服务器返回数据 的类 步骤3:创建 用于描述网络请求 的接口 步骤4:创建 Retrofit 实例 步骤5:创建 网络请求接口实例 并 配置网络请求参数 步骤6:发送网络请求(异步 / 同步) 步骤7: 处理服务器返回的数据 步骤讲解: 步骤1:添加Retrofit库的依赖 project下的build.gradle: implementation 'com.squareup.okhttp3:okhttp:3.8.1' //retrofit基于okhttp进行网络请求,所以这里添加它的依赖,都是square公司的 implementation 'com.squareup.retrofit2:retrofit:2.1.0' //retrofit2的库 implementation 'com.squareup.retrofit2:converter-gson:2.1.0' //配套的JSON解析库 同时清单的网络权限也不要忘记 AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET"/> 步骤2:创建 接收服务器返回JSON数据 的类(实体类/VO类的创建) public static class ResultsBean { //服务端返回的数据在前端/客户端对应的实体类 //下面的实例中我们都选择使用GsonFormat插件直接对JSON数据生成实体类 } 步骤3:创建 用于描述网络请求 的接口 public interface GnakApi { @GET("api/data/Android/10/1") Call<GankBean> getAndroidInfo(); //将官方的ResponseBody改成我们自己定的Gson实体类 这样响应请求以后 给回调方法返回的就是实体类了 //之后的实例里,对不同的实例,会使用不同的注解,不同的参数来对应各种不同情况下的请求 } 步骤4:创建 Retrofit 实例 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://gank.io/")//这里的url要和接口注解中的url拼接起来 http://gank.io/api/data/Android/10/1 .addConverterFactory(GsonConverterFactory.create()) .build(); 步骤5:创建 网络请求接口实例 //创建网络请求接口的实例 GnakApi api = retrofit.create(GnakApi.class); //对发送的请求进行封装 以及对实体类进行指定 Call<GankBean> call = api.getAndroidInfo(); 步骤6:发送网络请求(异步) 这里解释一下,这里不用写子线程是因为retrofit是基于okhttp的网络请求库,而okhttp的call回调中有一个namedrunnable是runnable接口的实现类,所以我们回调方法,即enqueue中的内容是在子线程中执行的。 call.enqueue(new Callback<GankBean>() {//这里回调有子线程 所以可以进行UI操作 @Override public void onResponse(Call<GankBean> call, Response<GankBean> response) { //请求成功时的逻辑 } @Override public void onFailure(Call<GankBean> call, Throwable t) { //请求失败时的逻辑 } }); 步骤7:处理返回数据(我之后的实例都是使用一个textview文本来取出服务器响应并返回的数据) call.enqueue(new Callback<GankBean>() {//这里回调有子线程 所以可以进行UI操作 @Override public void onResponse(Call<GankBean> call, Response<GankBean> response) { //我们对返回的GankBean实体类以及response响应数据进行回调函数编写操作 GankBean.ResultsBean bean = response.body().getResults().get(0); //以上代码将实体类bean与返回数据索引为0的数据对应 mTvResult.setText( "_id:" + bean.get_id() + "\n" + "createdAt:" + bean.getCreatedAt() + "\n" + "desc:" + bean.getDesc() + "\n" + "images:" + bean.getImages() + "\n" + "publishedAt:" + bean.getPublishedAt() + "\n" + "source:" + bean.getSource() + "\n" + "type:" + bean.getType() + "\n" + "url:" + bean.getUrl() + "\n" + "who:" + bean.getWho() + "\n"); } @Override public void onFailure(Call<GankBean> call, Throwable t) { } }); 三.实例讲解 1.正常get请求(http://gank.io/api/data/Android/10/1) 然后这是返回的JSON数据: { "error": false, "results": [ { "_id": "5b21f019421aa92a57e29e96", "createdAt": "2018-06-14T12:33:29.592Z", "desc": "\u53d7Android FAB\u7684\u542f\u53d1\u7684\u52a8\u753b\u9690\u85cf/\u663e\u793a\u89c6\u56fe\u3002", "images": [ "http://img.gank.io/228dcb82-b6cb-40bc-ac14-c2d909105176", "http://img.gank.io/97a2cf03-6aee-41c2-9179-72534d53de33", "http://img.gank.io/c6fc42e8-d13c-4b42-9771-74b7d46d69d9" ], "publishedAt": "2018-06-15T00:00:00.0Z", "source": "chrome", "type": "Android", "url": "https://github.com/3llomi/Hidely", "used": true, "who": "lijinshanmx" } } 步骤1略 步骤2创建 接收服务器返回数据 的类 (使用GsonFormat) GankBean: package com.example.sl.retrofitsample.Gson; import java.util.List; /** Created by sl on 2018/5/15. */ public class GankBean { private boolean error; private List<ResultsBean> results; public boolean isError() { return error; } public void setError(boolean error) { this.error = error; } public List<ResultsBean> getResults() { return results; } public void setResults(List<ResultsBean> results) { this.results = results; } public static class ResultsBean { /** * _id : 5a967b41421aa91071b838f7 * createdAt : 2018-02-28T17:49:53.265Z * desc : MusicLibrary-一个丰富的音频播放SDK * publishedAt : 2018-03-12T08:44:50.326Z * source : web * type : Android * url : https://github.com/lizixian18/MusicLibrary * used : true * who : lizixian * images : ["http://img.gank.io/90db2f35-2e9d-4d75-b5a9-53ee1719b57b"] */ private String _id; private String createdAt; private String desc; private String publishedAt; private String source; private String type; private String url; private boolean used; private String who; private List<String> images; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getPublishedAt() { return publishedAt; } public void setPublishedAt(String publishedAt) { this.publishedAt = publishedAt; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public boolean isUsed() { return used; } public void setUsed(boolean used) { this.used = used; } public String getWho() { return who; } public void setWho(String who) { this.who = who; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } } } 步骤3创建 用于描述网络请求 的接口(一般请求的选择就在这里使用注解体现出来,我们这里使用普通的get请求,不带参数) GankApi: public interface GnakApi { @GET("api/data/Android/10/1") Call<GankBean> getAndroidInfo();//将官方的ResponseBody改成我们自己定的Gson实体类 这样响应请求以后 给回调方法返回的就是实体类了 } 然后我们在MainActivity实现步骤4,5,6,7,并且使用按钮点击事件发送实例请求1,返回的数据使用id名为mTvResult的TextView空间显示 步骤4:创建Retrofit对象 步骤5:创建 网络请求接口 的实例 步骤6:发送网络请求 步骤7:处理返回数据 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://gank.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); //创建网络请求接口的实例 GnakApi api = retrofit.create(GnakApi.class); //对发送的请求进行封装 以及对实体类进行指定 Call<GankBean> call = api.getAndroidInfo(); call.enqueue(new Callback<GankBean>() {//这里回调有子线程 所以可以进行UI操作 @Override public void onResponse(Call<GankBean> call, Response<GankBean> response) { //我们对返回的GankBean实体类以及response响应数据进行回调函数编写操作 GankBean.ResultsBean bean = response.body().getResults().get(0); //以上代码将实体类bean与返回数据索引为0的数据对应 mTvResult.setText( "_id:" + bean.get_id() + "\n" + "createdAt:" + bean.getCreatedAt() + "\n" + "desc:" + bean.getDesc() + "\n" + "images:" + bean.getImages() + "\n" + "publishedAt:" + bean.getPublishedAt() + "\n" + "source:" + bean.getSource() + "\n" + "type:" + bean.getType() + "\n" + "url:" + bean.getUrl() + "\n" + "who:" + bean.getWho() + "\n"); } @Override public void onFailure(Call<GankBean> call, Throwable t) { } }); 然后我们演示下结果QAQ,就用个动图来显示吧~ 正常get请求.gif 2.拼接get请求(单个参数) 这里我们使用天气请求为例。我们使用查询串拼接http://op.juhe.cn/onebox/weather/query?cityname=深圳&key=XXXX 参数即为KEY 步骤1:添加Retrofit库的依赖 略 步骤2:创建 接收服务器返回数据 的类 WeatherDataBean:(代码过多,我们只看我们用到的) public class WeatherDataBean { private String reason; private ResultBean result; private int error_code; public ResultBean getResult() { return result; } public static class ResultBean { private DataBean data; public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { private RealtimeBean realtime; private List<WeatherBeanX> weather; public RealtimeBean getRealtime() { return realtime; } public static class RealtimeBean { private WeatherBean weather; public String getTime() { return time; } public void setTime(String time) { this.time = time; } public WeatherBean getWeather() { return weather; } } } 步骤3:创建 用于描述网络请求 的接口 WeatherApi: public interface WeatherApi { @GET("onebox/weather/query?cityname=深圳") Call<WeatherDataBean>getWeather(@Query("key")String key);//拼接查询串 我们这里只带了一个参数 } MainActivity完成以下步骤: 步骤4:创建 Retrofit 实例 步骤5:创建 网络请求接口实例 并 配置网络请求参数 步骤6:发送网络请求(异步 / 同步) 步骤7: 处理服务器返回的数据 Retrofit retrofit1 = new Retrofit.Builder() .baseUrl("http://op.juhe.cn/") .addConverterFactory(GsonConverterFactory.create()) .build(); WeatherApi api1 = retrofit1.create(WeatherApi.class);//创建这次请求的回调接口引用 Call<WeatherDataBean> call1 = api1.getWeather("4ea58de8a7573377cec0046f5e2469d5");//从这里 调用回调函数的时候 拼接url call1.enqueue(new Callback<WeatherDataBean>() { @Override public void onResponse(Call<WeatherDataBean> call, Response<WeatherDataBean> response) { String info = response.body().getResult().getData().getRealtime().getWeather().getInfo(); mTvResult.setText("深圳天气:" + info); } @Override public void onFailure(Call<WeatherDataBean> call, Throwable t) { t.printStackTrace(); } }); 然后看一下我们的结果: 单个参数.gif 3.拼接get请求(多个参数) //http://op.juhe.cn/onebox/weather/query?cityname=深圳&key=您申请的KEY 这里我们将cityname与key都设置为参数,来拼接我们的查询串(K-V方式) 步骤1:添加Retrofit库的依赖 略 步骤2:创建 接收服务器返回数据 的类 (仍旧是WeatherDataBean) 步骤3:创建 用于描述网络请求 的接口 WeatherApi1: public interface WeatherApi1 { @GET("onebox/weather/query?") Call<WeatherDataBean>getWeatherInfo(@QueryMap Map<String,String> params);//K-V键值对的形式 等会可以put多个参数 } MainActivity中完成: 步骤4:创建 Retrofit 实例 步骤5:创建 网络请求接口实例 并 配置网络请求参数 步骤6:发送网络请求(异步 / 同步) 步骤7: 处理服务器返回的数据 Retrofit retrofit3 = new Retrofit.Builder() .baseUrl("http://op.juhe.cn/") .addConverterFactory(GsonConverterFactory.create()) .build(); WeatherApi1 api12 = retrofit3.create(WeatherApi1.class);//将api转换成接口的形式 为了完成我们1发送请求以及2从接口返回数据的任务 Map<String, String> params = new HashMap<>(); params.put("cityname", "深圳");//这里使用K-V形式 params.put("key", "4ea58de8a7573377cec0046f5e2469d5"); Call<WeatherDataBean> call3 = api12.getWeatherInfo(params);//完成URL请求的拼接 call3.enqueue(new Callback<WeatherDataBean>() {//返回的数据回调处理 @Override public void onResponse(Call<WeatherDataBean> call, Response<WeatherDataBean> response) { String info = response.body().getResult().getData().getRealtime().getWeather().getInfo(); mTvResult.setText("深圳天气:" + info); } @Override public void onFailure(Call<WeatherDataBean> call, Throwable t) { } }); 然后我们看看效果: 多个参数.gif 4.get占位符请求 注解为我们提供了一种占位符方式,来拼接我们的url,适用于参数可选的情况http://gank.io/api/data/Android/10/1 其中Android/10/1: Android可接受参数 | Android | iOS | 休息视频 | 福利 | 前端 | App count 最大 50 page 是页数 这里我们使用占位符占位Page 步骤1:添加Retrofit库的依赖 略 步骤2:创建 接收服务器返回数据 的类 (上文的GankBean) 步骤3:创建 用于描述网络请求 的接口 GankApi1: //这里我们使用占位符占位Page public interface GnakApi1 { @GET("api/data/Android/10/{page}") Call<GankBean>getAndroidInfo1(@Path("page")int page);//登记回调函数 } MainActivity中: 步骤4:创建 Retrofit 实例 步骤5:创建 网络请求接口实例 并 配置网络请求参数 步骤6:发送网络请求(异步 / 同步) 步骤7: 处理服务器返回的数据 Retrofit retrofit2 = new Retrofit.Builder() .baseUrl("http://gank.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); GnakApi1 api11 = retrofit2.create(GnakApi1.class);//将api变成我们的java接口 Call<GankBean> call2 = api11.getAndroidInfo1(1);//传入参数 发送请求 //返回数据 将Gson实体类数据与服务器响应的api数据进行关联 在新建的回调中 call2.enqueue(new Callback<GankBean>() { @Override public void onResponse(Call<GankBean> call, Response<GankBean> response) { mTvResult.setText(response.body().getResults().get(0).getDesc()); } @Override public void onFailure(Call<GankBean> call, Throwable t) { t.printStackTrace(); } }); 然后我们看看效果: 占位符.gif 最后我们实现一个简单的post请求吧,别的博客中实现的是英汉互译之类的,我这里就直接对请求成功作一个json数据返回。 5.post请求 我这里使用https://www.mocky.io/创建一个restapi接口,模拟我们对post请求响应的数据。 post请求.JPG post请求1.JPG 然后我们可以看到它给我们生成的一个restful api接口:http://www.mocky.io/v2/5b27a2392e000010009e4497 步骤1:添加Retrofit库的依赖 略 步骤2:创建 接收服务器返回数据 的类 Result: public class Result { /** * result : yes */ private String result; public String getResult() { return result; } public void setResult(String result) { this.result = result; } } 步骤3:创建 用于描述网络请求 的接口 PostApi: public interface PostApi { @POST("v2/5b27a2392e000010009e4497") Call<Result> postUser(@Body User user);//这里我们返回的Result必定是yes 但是上传的User对象 并没有后台处理 } MainActivity: 步骤4:创建 Retrofit 实例 步骤5:创建 网络请求接口实例 并 配置网络请求参数 步骤6:发送网络请求(异步 / 同步) 步骤7: 处理服务器返回的数据 Retrofit retrofit4 = new Retrofit.Builder() .baseUrl("http://www.mocky.io/") .addConverterFactory(GsonConverterFactory.create()) .build(); PostApi api13 = retrofit4.create(PostApi.class); User user = new User(); user.setId(1); user.setName("lgl"); Call<Result> call4 = api13.postUser(user);//这里 我们上传的是user对象,返回的是JSON数据的result 而后用GSON解析 call4.enqueue(new Callback<Result>() { @Override public void onResponse(Call<Result> call, Response<Result> response) { if (response.body().getResult().equals("OK")) { mTvResult.setText(response.body().getResult()); Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<Result> call, Throwable t) { } }); 如果成功,那么我们显示一个Toast并且修改TextView内容: post请求.gif 以上就是实例的所有内容。代码地址。我的github:Ricardo-L-Song/retrofitsample 喜欢就给颗小吧~

优秀的个人博客,低调大师

CentOS下Nginx 配置多站点vhost

假设你想在Linux Nginx中用不同的域名访问不同的目录,这时就要配置多个vhost,具体配置如下,假设网站根目录设定在/var/www/ 1、在/var/www/下新建两个目录 /var/www/ushark.net /var/www/ushark.wang 2、编辑/etc/nginx/nginx.conf http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; #主要是加入此行,如有则忽略 } 3、在/etc/nginx/conf.d下新建两个conf文件, /etc/nginx/conf.d/ushark.net.conf /etc/nginx/conf.d/ushark.wang.conf 4、复制如下配置信息到两个文件中,只要修改红色部分内容 !!! server_name与root保持一致即目录和域名一一对应 !!! server { listen 80; server_name www.ushark.net; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; root /var/www/ushark.net/; if (!-e $request_filename){ # rewrite可根据网站需要增删 rewrite ^/(.*) /index.php last; } location / { index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/ushark.net/; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } 5、重启Nginx systemctl restart nginx 6、 编辑/etc/hosts !!! 核心步骤 !!! [root@bogon ~]# vi 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 127.0.0.1 www.ushark.net 127.0.0.1 www.ushark.wang 7、设置成功

优秀的个人博客,低调大师

大数据产业迎多政策支持

记者从国家发改委等部门获悉,决策层将出台政策“组合拳”,支持大数据产业发展,其中包括构建国家大数据平台、编制大数据产业规划等。 政府数据统一开放平台2018年建成 作为战略新兴产业重要分支之一,大数据产业正迎来政策层面的诸多支持。其中,《促进大数据发展行动纲要》已在2015年印发。 “2016数博会已升格为国家级盛会。除此之外,近日工信部信息化和软件服务业司司长谢少锋透露,正在制定大数据产业‘十三五’发展规划,预计在今年下半年发布。”分析人士刘阳说,决策层对大数据的政策助力会越来越多。 记者也从国家发改委等部门了解到,决策层对于支持大数据产业将有一系列的“组合拳”,包括国家大数据平台、大数据产业规划、大数据产业链细化政策以及数据可视化的技术攻关等。 “2017年底前,我国将构建跨部门的政府数据共享交换平台;2018年底前,建成国家政府数据统一开放平台。”国家发改委相关人士对记者表示,国家大数据平台是由国家主导、服务全国的大数据平台,是国家重要基础信息设施。 据悉,该平台包括数据共享交换、数据开放等基础支撑平台以及数据汇聚分析等应用平台。 大数据产业迎来爆发期 “中国大数据应用市场快速增长,2015年市场规模46亿元,同比增长133%。2016年有望突破百亿规模。”刘阳分析认为,大数据产业正迎来爆发期。 记者也注意到,一些企业的发展印证了大数据产业进入快速发展阶段的事实。以挂牌新三板的数据堂为例,其2015年度实现营业收入6814.53万元,同比增长270.86%;净利润580.60万元,同比增长249.31%。 事实上,大数据已经渗透到每一个行业和业务职能领域,物联网、云计算、移动互联网、车联网、手机、平板电脑、PC以及遍布各个角落的各种各样的传感器,无一不是大数据来源或者承载的方式。 “大数据将对国家治理模式、企业的决策、组织和业务流程、个人生活方式等产生巨大的影响。”刘阳说,信息技术发展到现阶段,未来的很多行业都是和大数据结合做解决方案,大数据产业的爆发将是必然趋势。 本文转自d1net(转载)

优秀的个人博客,低调大师

数据驱动的多策略聊天引擎

1. 聊天引擎做什么 聊天引擎是一款模拟人类对话或聊天的陪伴产品。接受用户的自然语言输入,返回可解释、承上启下、顺畅的自然语言句子。与问答引擎、专家系统等不同,聊天引擎重点在“侃”,不太关注如何解决用户的实际问题或者执行任务,而是关注如何能与用户进行天南地北的聊天,涉及的话题会很广,但并不需要有精准的答案,重在互动的有效性和可持续性。聊天系统在对儿童和老人的陪伴方面可以起到非常重要的作用,即便在一个以帮助用户解决实际问题或寻找精准答案的个人助手类产品中,具有开放式聊天能力也是至关重要的。这样的聊天能力,可以成为各种信息查询、任务完成等能力之间的润滑剂,很好的改善人机交互过程中的用户体验,增强助理型机器人的人性化和用户粘性。 苹果公司于2011年10月随iphone4s一起发布了Siri,轰动一时,重新点燃了人们对语音助理的期望

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册