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

一个空间怎么放多个网站吗百度引擎搜索引擎

一个空间怎么放多个网站吗,百度引擎搜索引擎,海南建设银行分行网站,wordpress注册提示Retrofit是由Square公司开发的类型安全HTTP客户端框架,借助动态代理在运行时生成接口实现类,将注解转化为OkHttp请求配置;节省成本通过转换器(Gson/Moshi)自动序列化JSON/XML,内部处理网络请求在主线程返回报文。Retrofit 直译是封…

Retrofit是由Square公司开发的类型安全HTTP客户端框架,借助动态代理运行时生成接口实现类将注解转化为OkHttp请求配置节省成本通过转换器(Gson/Moshi)自动序列化JSON/XML内部处理网络请求在主线程返回报文。Retrofit 直译是封装、翻版。他就是对okhttp做了进一步封装,方便使用,它底层的所有请求默认走的都是Okhttp。  所以使用Retrofit必须依赖okHttp。

两者区别:Retrofit是基于App发请求的封装,也就是面向的应用层(比如响应数据的处理和错误处理等),而Okhhtp是对底层网络请求的封装与优化(socket优化,数据压缩,buffer缓存等)

本文将通过GET/POSTde 基础请求、文件上传、动态URL、缓存转换器,以及借助OKHttp实现Retrofit请求过程中设置请求头、cookie、超时时间和拦截器都是借助OkHttpClient 等。

1、添加依赖:

使用Retrofit添加的依赖,另外,使用转换器也需要在build.gradle中单独引入,两种转换器通常不同时使用,根据项目JSON处理需求选择其一即可

dependencies {implementation 'com.squareup.retrofit2:retrofit:2.9.0'
}

GsonConverterFactory依赖配置

implementation 'com.squareup.retrofit2:converter-gson:2.9.0' 
implementation 'com.google.code.gson:gson:2.8.8' 

MoshiConverterFactory依赖配置: 

    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' 
    implementation 'com.squareup.moshi:moshi:1.14.0' // Moshi

      2、业务场景实现

      先定义一个接口:

      切记这里必须是interface!!!

      public interface RequestService {@GET("login/{id}")Call<LoginInfo> login(@Path("id") int userId);@POST("users/new")@Headers("Content-Type: application/json") // 设置请求头Call<ResponseBody> loginPost(@Body LoginInfo loginInfo); // @Body注解表示请求体@Multipart // 表示多部分请求@POST("upload")Call<ResponseBody> uploadFile( @Part("description") RequestBody description,  @Part MultipartBody.Part file);// 借助@Url注解,指定完整URL@GETCall<ResponseBody> getCustomUrl(@Url String url);//借助Query注解,添加查询参数@GET("search")Call<ResponseBody> queryData(@Query("q") String query);}

      定义实体类 LoginInfo.java

      public class LoginInfo {private String userNmae;private String userPwd;public LoginInfo(String userNmae, String userPwd) {this.userNmae = userNmae;this.userPwd = userPwd;}
      }

      定义初始化Retrofit的单例工具类 RetrofitUtil.java

      public class RetrofitUtil {private static RetrofitUtil retrofitUtil;private Retrofit retrofit;File httpCacheDirectory = new File(BaseApplication.getContext().getCacheDir(), "responses");int cacheSize = 10 * 1024 * 1024; // 10MBCache cache = new Cache(httpCacheDirectory, cacheSize);private RetrofitUtil(){}public static RetrofitUtil getInstance(){synchronized (RetrofitUtil.class){if (retrofitUtil == null){retrofitUtil = new RetrofitUtil();}return retrofitUtil;}}public <T> T getServer(Class<T> cls, String baseUrl){if (retrofit == null){retrofit = new Retrofit.Builder().baseUrl(baseUrl).client(getClient()).addConverterFactory(GsonConverterFactory.create()).build();}return retrofit.create(cls);}public OkHttpClient getClient(){// 创建OkHttpClientOkHttpClient okHttpClient = new OkHttpClient.Builder()
      //                .cookieJar().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).addInterceptor(new Interceptor() {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();// 添加统一请求头request = request.newBuilder().header("Cache-Control", "public, max-age=60").build();return chain.proceed(request);}}).cache(cache).build();return okHttpClient;}
      }

      注意Retrofit在设置请求头、cookie、超时时间和拦截器都是借助OkHttpClient 来实现。

      2.1 Get请求

      getTv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int id = 600001;RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).login(id).enqueue(new Callback<LoginInfo>() {@Overridepublic void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {if (response.isSuccessful()) {LoginInfo loginInfo = response.body(); // 自动解析JSON为User对象}}@Overridepublic void onFailure(Call<LoginInfo> call, Throwable t) {t.printStackTrace();}});}
      });

      2.2 POST请求

      LoginInfo loginInfo = new LoginInfo("Admin", "12345678");
      // 发起请求
      Call<ResponseBody> call = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).loginPost(loginInfo);
      call.enqueue(new Callback<ResponseBody>() {@Overridepublic void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {// 请求成功}@Overridepublic void onFailure(Call<ResponseBody> call, Throwable t) {// 请求失败}
      });

      2.3 文件上传

      // 准备文件
      File file = new File("path/to/file.jpg");
      RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
      MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);// 发起上传请求
      RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).uploadFile(RequestBody.create(MediaType.parse("text/plain"), "文件描述"),body).enqueue(new Callback<ResponseBody>() {@Overridepublic void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {//处理上传成功后的逻辑}@Overridepublic void onFailure(Call<ResponseBody> call, Throwable t) {//处理上传失败的逻辑}
      });

      2.4动态指定URL和添加查询参数

      一般来说一个项目中你可能定封装了公共个请求路径和域名,但有个别接口就需要特立独行的地址和传参,此时可以借助@Url和@Query动态的实现。 两个请求也在上面给出的RequestService 这个接口中定义了。

              // 使用动态URLCall<ResponseBody> call1 = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).getCustomUrl("https://api.example.com/custom/path");// 使用查询参数Call<ResponseBody> call2 = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).queryData("retrofit");

      个人总结记录,才疏学浅,如有错误,欢迎指正,多谢。 

      http://www.dtcms.com/wzjs/18981.html

      相关文章:

    1. 网站开发设计心得及体会必应搜索引擎网址
    2. dw自己做的网站手机进不去优秀软文营销案例
    3. 淮南网站推广做电商必备的几个软件
    4. 专业型网站网站国内十大4a广告公司
    5. 浙江省电子商务网站建设北京seo全网营销
    6. 凡科网站做的作品如何发布考研培训
    7. 做网站开发的营业执照新手怎么引流推广推广引流
    8. 成都网站建设名录seo排名优化工具推荐
    9. 南昌做任务的网站东莞网站优化关键词排名
    10. 有什么网站可以做批发刷赞业务推广网站
    11. 北京联通网站备案推广产品的渠道
    12. 什么网站做国外批发上海网络营销上海网络推广
    13. 国外的一个大学生做的匿名社交网站长沙网站公司品牌
    14. 博客可以做网站收录用的吗合肥百度快照优化排名
    15. 怎样下载门户网站小程序开发平台官网
    16. asp做的网站数据库在哪里建站之星网站
    17. 做调查问卷能赚钱的网站网站建设方案书范文
    18. 有什么做视频的免费素材网站淘宝店铺运营
    19. 用flash做的网站有哪些百度收录网站链接入口
    20. 创业给企业做网站开发百度平台客服联系方式
    21. 安通建设有限公司网站重庆百度推广关键词优化
    22. 四川微信网站建设公google浏览器官网入口
    23. 企业管理软件开发平台上海seo优化
    24. 织梦做响应式网站seo数据监控平台
    25. 怎么做直播网站刷弹幕乔拓云建站平台
    26. 网站建设数据库怎么选择百度快速优化软件
    27. 怎么在网站上添加地图自己怎么做一个网页
    28. 网站设计公司排名知乎网站推广软件费用是多少
    29. wordpress 实例农大南路网络营销推广优化
    30. 网站怎么做排查修复安卓优化大师app