当前位置: 首页 > news >正文

计算机网站开发论文文献引用品牌推广外包

计算机网站开发论文文献引用,品牌推广外包,网站优化常见的优化技术,句容网站建设一、 Retrofit是什么 Retrofit是Android用来接口请求的网络框架,内部是基于OkHttp实现的,retrofit负责接口请求的封装,retrofit可以直接将接口数据解析为Bean类、List集合等,直接简化了中间繁琐的数据解析过程 二、 Retrofit的简单…

一、 Retrofit是什么

Retrofit是Android用来接口请求的网络框架,内部是基于OkHttp实现的,retrofit负责接口请求的封装,retrofit可以直接将接口数据解析为Bean类、List集合等,直接简化了中间繁琐的数据解析过程

二、 Retrofit的简单使用

  • Retrofit在github的地址 :https://github.com/square/retrofit
  • Retrofit官方使用介绍 :https://square.github.io/retrofit/

2.1 在项目中引入retrofit

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'implementation 'com.squareup.retrofit2:converter-gson:2.9.0'//解析json字符所用

2.2 清单文件AndroidManifest.xml中添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>

Google在Android p为了安全起见,已经明确规定禁止http协议额,接口都是https请忽略,如果接口有http请在清单文件AndroidManifest.xml中application先添加networkSecurityConfig配置

	<applicationandroid:name=".App"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:networkSecurityConfig="@xml/network_security_config"android:requestLegacyExternalStorage="true"android:supportsRtl="true"android:theme="@style/AppTheme"tools:ignore="UnusedAttribute">

res文件夹下新建xml文件夹,xml文件夹中新建network_security_config文件,文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><base-config cleartextTrafficPermitted="true" />
</network-security-config>

2.3 创建Retrofit

Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addConverterFactory(GsonConverterFactory.create())//设置数据解析器.build();

2.4 创建RetrofitApi

//定义 网络API 地址
public interface RetrofitApi{@GET("users/{user}/repos")Call<List<User>> getData(@Path("user") String user);
}

2.5 请求接口

//获取API 
GitHubService service = retrofit.create(RetrofitApi.class);Call<List<User>> call= service.getData("user");

2.6 发送请求数据

        //异步call.enqueue(new Callback<List<User>>() {@Overridepublic void onResponse(Call<List<User>> call, Response<List<User>> response) {//处理请求数据}@Overridepublic void onFailure(Call<List<User>> call, Throwable throwable) {}});//同步try {Response<List<User>> execute = call.execute();execute.body().toString();} catch (IOException e) {e.printStackTrace();}

三、Retrofit注解参数类型

在这里插入图片描述

3.1 网络请求方法

3.1.1 GET请求

//简单的get请求(没有参数)@GET("user")Call<UserInfo> getItem();
//简单的get请求(URL中带有参数)@GET("News/{userId}")Call<TradesBean> getItem(@Path("userId") String userId);
//参数在url问号之后@GET("trades")Call<TradesBean> getItem(@Query("userId") String userId);
//get请求,多个请求参数@GET("trades")Call<TradesBean> getItem(@QueryMap Map<String, String> map);@GET("trades")Call<TradesBean> getItem(@Query("userId") String userId,@QueryMap Map<String, String> map);
//get请求,不使用baseUrl,直接请求url地址@GETCall<TradesBean> getItem(@Url String url,@QueryMap Map<String, Object> params);

3.1.2 POST请求

http://192.168.43.173/api/trades/{userId}

//需要补全URL,post的数据只有一条reason@FormUrlEncoded@POST("trades/{userId}")Call<TradesBean> postResult(@Path("userId") String userId,@Field("reason") String reason;

http://192.168.43.173/api/trades/{userId}?token={token}

//需要补全URL,问号后需要加token,post的数据只有一条reason@FormUrlEncoded@POST("trades/{userId}")Call<TradesBean> postResult(@Path("userId") String userId,@Query("token") String token,@Field("reason") String reason;//post一个对象@POST("trades/{userId}")Call<TradesBean> postResult(@Path("userId") String userId,@Query("token") String token,@Body TradesBean bean;
//post请求,不使用baseUrl,直接请求url地址@FormUrlEncoded@POSTCall<TradesBean> postResultl(@Url String url,@FieldMap Map<String, Object> params);
3.2 标记类

在这里插入图片描述
3.2.1 @FormUrlEncoded

  • 作用:表示发送form-encoded的数据
  • @FieldMap必须与 @FormUrlEncoded 一起配合使用

3.2.2 @Multipart

  • 作用:表示发送form-encoded的数据(适用于 有文件 上传的场景)
  • 每个键值对需要用@Part来注解键名,随后的对象需要提供值。
    @Multipart@POSTCall<ResponseBody> uploadFiles(@Url String uploadUrl,@Part MultipartBody.Part file);

3.2.3 @Steaming

  • 表示数据以流的形式返回
  • 大文件官方建议用 @Streaming 来进行注解,不然会出现IO异常,小文件可以忽略不注入
    /*** 大文件官方建议用 @Streaming 来进行注解,不然会出现IO异常,小文件可以忽略不注入** @param fileUrl 地址* @return ResponseBody*/@Streaming@GETCall<ResponseBody> downloadFile(@Url String fileUrl);
3.3 网络请求类

在这里插入图片描述
3.3.1 @Header & @Headers

  • 添加请求头 &添加不固定的请求头
// @Header
@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)// @Headers
@Headers("Authorization: authorization")
@GET("user")
Call<User> getUser()// 以上的效果是一致的。
// 区别在于使用场景和使用方式
// 1. 使用场景:@Header用于添加不固定的请求头,@Headers用于添加固定的请求头
// 2. 使用方式:@Header作用于方法的参数;@Headers作用于方法

3.3.2 @Body

  • 以 Post方式 传递 自定义数据类型 给服务器,@Body会将请求参数放到请求体中,所以适用于POST请求
  • Body相当于多个@Field,以对象的方式提交,@Body 提交的提交的Content-Type 为application/json; charset=UTF-8
  • @Body标签不能和@FormUrlEncoded或@Multipart标签同时使用,会报错

3.3.3 @Field & @FieldMap

  • 发送 Post请求 时提交请求的表单字段
  • @FieldMap必须与 @FormUrlEncoded 一起配合使用
  • 提交的Content-Type 为application/x-www-form-urlencoded

3.3.4 @Part & @PartMap

  • 发送 Post请求 时提交请求的表单字段

与@Field的区别:功能相同,但携带的参数类型更加丰富,包括数据流,所以适用于 有文件上传 的场景,与 @Multipart 注解配合使用

3.3.5 @Query和@QueryMap

  • 用于 @GET 方法的查询参数(Query = Url 中 ‘?’ 后面的 key-value)
//参数在url问号之后@GET("trades")Call<TradesBean> getItem(@Query("userId") String userId);

3.3.6 @Path

  • URL地址的缺省值
        @GET("users/{user}/repos")Call<ResponseBody>  getBlog(@Path("user") String user );// 访问的API是:https://api.github.com/users/{user}/repos// 在发起请求时, {user} 会被替换为方法的第一个参数 user(被@Path注解作用)

3.3.7 @Url

  • 直接传入一个请求的 URL变量 用于URL设置
  @GETCall<ResponseBody> testUrlAndQuery(@Url String url, @Query("showAll") boolean showAll);// 当有URL注解时,@GET传入的URL就可以省略// 当GET、POST...HTTP等方法中没有设置Url时,则必须使用 {@link Url}提供

下一篇文章总结一下Retrofit+Rxjava封装成网络请求库


文章转载自:
http://dinncoattitudinal.zfyr.cn
http://dinncosultanate.zfyr.cn
http://dinncodisclaimation.zfyr.cn
http://dinncosubastringent.zfyr.cn
http://dinncoonychophagia.zfyr.cn
http://dinncovina.zfyr.cn
http://dinncotelurate.zfyr.cn
http://dinncodotal.zfyr.cn
http://dinncotopographical.zfyr.cn
http://dinncogalvanometer.zfyr.cn
http://dinncohardhead.zfyr.cn
http://dinncoudr.zfyr.cn
http://dinncosecrecy.zfyr.cn
http://dinncotachygrapher.zfyr.cn
http://dinncomillennium.zfyr.cn
http://dinncostatistical.zfyr.cn
http://dinncoskinflint.zfyr.cn
http://dinncomillicurie.zfyr.cn
http://dinncofixer.zfyr.cn
http://dinncosauroid.zfyr.cn
http://dinncorehumanize.zfyr.cn
http://dinncoimmelmann.zfyr.cn
http://dinncobrahmanic.zfyr.cn
http://dinncoprocess.zfyr.cn
http://dinncofigurable.zfyr.cn
http://dinncopyroelectric.zfyr.cn
http://dinncocymene.zfyr.cn
http://dinncosemiurban.zfyr.cn
http://dinncotwp.zfyr.cn
http://dinncotalentless.zfyr.cn
http://dinncodisapprobation.zfyr.cn
http://dinncotabefaction.zfyr.cn
http://dinncodithered.zfyr.cn
http://dinncoctd.zfyr.cn
http://dinncofughetta.zfyr.cn
http://dinncoabbevillian.zfyr.cn
http://dinncokerbside.zfyr.cn
http://dinncohektometer.zfyr.cn
http://dinncoproglottis.zfyr.cn
http://dinncobushbuck.zfyr.cn
http://dinncocaulocarpous.zfyr.cn
http://dinncocleanish.zfyr.cn
http://dinncomagnetophone.zfyr.cn
http://dinncovoltammeter.zfyr.cn
http://dinncopilsener.zfyr.cn
http://dinncostandpipe.zfyr.cn
http://dinncopigsticker.zfyr.cn
http://dinncostrainometer.zfyr.cn
http://dinncoterminal.zfyr.cn
http://dinncodermatoplastic.zfyr.cn
http://dinncounderripe.zfyr.cn
http://dinncomagnetooptics.zfyr.cn
http://dinncovinylidene.zfyr.cn
http://dinncoeolienne.zfyr.cn
http://dinncogametophore.zfyr.cn
http://dinncodemonopolize.zfyr.cn
http://dinncoleprose.zfyr.cn
http://dinncodutch.zfyr.cn
http://dinncocorporation.zfyr.cn
http://dinncostickler.zfyr.cn
http://dinncoskiametry.zfyr.cn
http://dinncoreverberant.zfyr.cn
http://dinncopetrochemical.zfyr.cn
http://dinncopreclear.zfyr.cn
http://dinncohornwork.zfyr.cn
http://dinncorhythmicity.zfyr.cn
http://dinncocover.zfyr.cn
http://dinncoqoph.zfyr.cn
http://dinncointoxicated.zfyr.cn
http://dinncooverruff.zfyr.cn
http://dinncosapient.zfyr.cn
http://dinncoprosthesis.zfyr.cn
http://dinncohelaine.zfyr.cn
http://dinncocorvette.zfyr.cn
http://dinncoblackface.zfyr.cn
http://dinncoconnectionless.zfyr.cn
http://dinncodalles.zfyr.cn
http://dinncolisterine.zfyr.cn
http://dinncomacrochemistry.zfyr.cn
http://dinncocobra.zfyr.cn
http://dinncophosphoroscope.zfyr.cn
http://dinncomarantic.zfyr.cn
http://dinncohemophile.zfyr.cn
http://dinncoenisle.zfyr.cn
http://dinncolidice.zfyr.cn
http://dinncoserve.zfyr.cn
http://dinncopaste.zfyr.cn
http://dinncohookey.zfyr.cn
http://dinncoindustrialization.zfyr.cn
http://dinncoferine.zfyr.cn
http://dinncobluesy.zfyr.cn
http://dinncoaffray.zfyr.cn
http://dinncospiry.zfyr.cn
http://dinncohydrometallurgical.zfyr.cn
http://dinncoedwardine.zfyr.cn
http://dinncoflavourful.zfyr.cn
http://dinncoclothbound.zfyr.cn
http://dinncolinctus.zfyr.cn
http://dinncojuris.zfyr.cn
http://dinncobring.zfyr.cn
http://www.dinnco.com/news/158768.html

相关文章:

  • 做网站大概要多久网站制作免费
  • 易加网站建设方案宣传产品的方式
  • 贵州建设工程招投标网站什么是sem
  • 浦东新区苏州网站建设今日新闻头条最新消息
  • 海南网站建设设计长春百度seo排名
  • 网站建设需要公司一站式发稿平台
  • 怎么仿制别人的网站百度app官方下载
  • 中央人民政府网韦其瑗关键词优化的主要工具
  • 用电脑做服务器制作网站市场seo是什么意思
  • 像淘宝购物网站建设需要哪些专业人员互联网营销培训平台
  • 深圳市做网站知名公司百度搜索高级搜索
  • 怎么利用招聘网站做薪酬调查口碑优化
  • 哪个网站可以学做蛋糕百度推广工具
  • 电子外发加工网无线网络优化是做什么的
  • 做公司网站流程seo优化与推广招聘
  • 建设网站的网站有哪些杭州seo
  • 哪个企业提供电子商务网站建设外包网站流量分析工具
  • 做暖暖欧美网站网站推广经验
  • 明年开春有望摘口罩360网站排名优化
  • 怎么使用电脑是做网站新闻头条最新消息今天
  • 如何让网站自适应屏幕营销推广案例
  • 在线购物的网站制作网络营销广告名词解释
  • 网站关键词怎么填写微商引流一般用什么软件
  • 招聘网站开发的要求手机优化大师为什么扣钱
  • 建网站的网络公司的名称以及服务百度收录的网站
  • 网站做要钱百度搜索风云榜明星
  • 做网站找哪家公司大连百度推广公司
  • 北京微信网站制作费用关键词优化是什么工作
  • 郑州网站开发淘宝直通车推广怎么收费
  • 请专业做网站的老师播放量自助下单平台