苹果x翻新机序列号开头

In this tutorial, we’ll be implementing RxJava with Retrofit such that the Retrofit Service is called every x seconds in our Android Application.

在本教程中,我们将使用Retrofit实现RxJava,以便在Android应用程序中每x秒调用一次Retrofit服务。

Android RxJava和改造 (Android RxJava and Retrofit)

We have already discussed the basics of RxJava and Retrofit together, here.

我们已经在这里一起讨论了RxJava和Retrofit的基础。

In order to create a Retrofit service that runs after certain time intervals, we can use the following :

为了创建在特定时间间隔后运行的Retrofit服务,我们可以使用以下命令:

  • Handlers处理程序
  • RxJavaRxJava的

Handlers are used to communicate/pass data from the background thread to the UI thread

处理程序用于将数据从后台线程传递/传递到UI线程

Using RxJava we can do much more than that and very easily as well, using RxJava operators.
We can use the interval operator to call a certain method ( retrofit network call in our case) after every given period.

使用RxJava,我们可以使用RxJava运算符做更多的事情,而且非常容易。
在每个给定时间段之后,我们可以使用间隔运算符来调用某种方法(在我们的情况下为翻新网络调用)。

Observable.interval operator is used to emit values after certain intervals. It looks like this:

Observable.interval运算符用于在特定间隔后发出值。 看起来像这样:

Observable.interval(1000, 5000,TimeUnit.MILLISECONDS);

1000 is the initial delay before the emission starts and repeats every 5 seconds.

1000是发射开始之前的初始延迟,每5秒重复一次。

We can subscribe our observers which would call the Retrofit method after every 5 seconds.

我们可以订阅观察者,该观察者每5秒就会调用Retrofit方法。

Calling Retrofit service after certain intervals is fairly common in applications that provide live updates, such as Cricket Score application etc.

在某些时间间隔后调用翻新服务在提供实时更新的应用程序中非常普遍,例如板球得分应用程序等。

Let’s get started with our implementation in the following section. We’ll be creating an application which shows a new random joke in the TextView after every 5 seconds.

在下一节中,让我们开始实施。 我们将创建一个应用程序,该应用程序每隔5秒就会在TextView中显示一个新的随机笑话。

项目结构 (Project Structure)

Add the following dependencies to the build.gradle file:

将以下依赖项添加到build.gradle文件:

implementation('com.squareup.retrofit2:retrofit:2.3.0'){exclude module: 'okhttp'}implementation 'io.reactivex.rxjava2:rxjava:2.1.9'implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

In order to use lambda syntax, make sure that the compile options are set as the following inside the android block in the build.gradle.

为了使用lambda语法,请确保在build.gradle的android块内将编译选项设置为以下内容。

android
{
...
compileOptions {targetCompatibility 1.8sourceCompatibility 1.8}
...
}

码 (Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:padding="16dp"android:text="See this space for more jokes..."app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="24dp"android:text="JOKES BELOW...."android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

We’ll be using the following public API:
https://api.chucknorris.io/jokes/random.

我们将使用以下公共API:
https://api.chucknorris.io/jokes/random 。

Create an APIService.java class where the API is defined:

创建定义了API的APIService.java类:

package com.journaldev.androidretrofitcalleveryxsecond;import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Path;public interface APIService {String BASE_URL = "https://api.chucknorris.io/jokes/";@GET("{path}")Observable<Jokes> getRandomJoke(@Path("path") String path);
}

Following is the POJO model for the Jokes.java class :

以下是Jokes.java类的POJO模型:

package com.journaldev.androidretrofitcalleveryxsecond;import com.google.gson.annotations.SerializedName;import java.util.List;public class Jokes {@SerializedName("url")public String url;@SerializedName("icon_url")public String icon_url;@SerializedName("value")public String value;
}

The code for the MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidretrofitcalleveryxsecond;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;import com.google.gson.Gson;
import com.google.gson.GsonBuilder;import java.util.concurrent.TimeUnit;import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;import static com.journaldev.androidretrofitcalleveryxsecond.APIService.BASE_URL;public class MainActivity extends AppCompatActivity {Retrofit retrofit;TextView textView;APIService apiService;Disposable disposable;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textView);HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).connectTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).build();Gson gson = new GsonBuilder().setLenient().create();retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(client).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create(gson)).build();apiService = retrofit.create(APIService.class);disposable = Observable.interval(1000, 5000,TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(this::callJokesEndpoint, this::onError);}@Overrideprotected void onResume() {super.onResume();if (disposable.isDisposed()) {disposable = Observable.interval(1000, 5000,TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(this::callJokesEndpoint, this::onError);}}private void callJokesEndpoint(Long aLong) {Observable<Jokes> observable = apiService.getRandomJoke("random");observable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).map(result -> result.value).subscribe(this::handleResults, this::handleError);}private void onError(Throwable throwable) {Toast.makeText(this, "OnError in Observable Timer",Toast.LENGTH_LONG).show();}private void handleResults(String joke) {if (!TextUtils.isEmpty(joke)) {textView.setText(joke);} else {Toast.makeText(this, "NO RESULTS FOUND",Toast.LENGTH_LONG).show();}}private void handleError(Throwable t) {//Add your error here.}@Overrideprotected void onPause() {super.onPause();disposable.dispose();}
}

setLenient() is used to prevent Misinformed JSON response exceptions.

setLenient()用于防止错误的JSON响应异常。

The disposable instance is unsubscribed when the user leaves the activity.

当用户离开活动时,该一次性实例将取消订阅。

Check the isDisposed method on the Disposable before creating the Observable stream again in the onResume method.

onResume方法中再次创建Observable流之前,请检查Disposable上的isDisposed方法。

The output of the application in action is given below:

实际应用程序的输出如下:

That brings an end to this tutorial. You can download the project from the link below:

这样就结束了本教程。 您可以从下面的链接下载项目:

AndroidRetrofitCallEveryXSecondAndroidRetrofitCallEveryXSecond
AndroidRetrofitCallEveryXSecondAndroidRetrofitCallEveryXSecond

翻译自: https://www.journaldev.com/23007/android-retrofit-call-every-x-seconds

苹果x翻新机序列号开头

苹果x翻新机序列号开头_Android翻新电话每隔X秒相关推荐

  1. 苹果x和xsmax有什么区别_手机资讯:Apple 认证的翻新产品是什么苹果官方翻新机和全新设备有什么区别...

    如今使用IT数码设备的小伙伴们是越来越多了,那么IT数码设备当中是有很多知识的,这些知识很多小伙伴一般都是不知道的,就好比最近就有很多小伙伴们想要知道Apple 认证的翻新产品是什么苹果官方翻新机和全 ...

  2. 苹果台式电脑怎么开机_龙华苹果电脑回收公司,台式电脑回收公司电话

    龙华苹果电脑回收公司,台式电脑回收公司电话oDYIHx    通常液晶显示器有VGA和DVI两种种接口,其中VGA接口在长时间显示后悔出现画面模糊情况,需要校正才能恢复,然而DVi接口传输就比较稳定, ...

  3. 苹果7pnfc功能门禁卡_苹果下个月终于要开放NFC权限了!iphone一秒变身门禁卡

    原标题:苹果下个月终于要开放NFC权限了!iphone一秒变身门禁卡 当我们开始习惯用iPhone的NFC功能进行移动支付,苹果公司要进一步开放NFC芯片功能,它将能用来充当钥匙,可以打开房门车门. ...

  4. iphonex 序列号_iPhoneX怎么看序列号?苹果iPhoneX查看序列号的三种方法

    对于后续准备入手iPhoneX的果粉来说,很多朋友拿到真机之后都想知道自己买到是不是正品.水货之类的.其实一般情况下除了通过外观真机.配件之外去辨别之外,我们还可以通过iPhoneX序列号去查看.对于 ...

  5. 苹果雪豹操作系统正式版_Android 11 正式版发布!

    整理 | 郑丽媛 出品 | CSDN(ID:CSDNnews) 头图 | CSDN 下载自谷歌官网 Android 11今天正式发布了!新版本主要加强了聊天气泡.安全隐私.电源菜单,以及对瀑布屏.折叠 ...

  6. 苹果软件更新在哪里_【软件更新】安卓秒变苹果主题软件

    在安卓手机中装上苹果主题!新版iosX主题就是来帮助你协调这一切的一款App.之前G先生发布了一篇ios主题「https://mp.weixin.qq.com/s/jS8M8uyWeUyFnQb055 ...

  7. android 每隔2秒执行_Android中实现延迟执行操作的三种方法

    今天在敲代码的过程中,有个需求是延迟执行某方法. 整理收集了三种方法,自己用的是第三种. 第一种线程休眠:new Thread() { @Override public void run() { su ...

  8. 医院网站推广的基本策略

    现今,中国民营医院对医疗企划.推广营销方面非常重视,有些医院不珍重金招聘专业人才,把重点放到了医院网站推广上,由此掀起一场网络推广大战.如今医院网站越来越多,竞争也势必也越来越激烈.那么如何在激烈的医 ...

  9. 2022亚太杯A题思路-序列图像特征提取及模具熔融结晶建模分析

    完整解答获取见文末 序列图像特征提取及模具熔融结晶建模分析 连铸过程中的模具通量对钢半月板进行热绝缘,防止液态钢连铸过程中液态钢再 氧化,控制传热,提供链润滑,吸收非金属夹杂物.模具通量的冶金功能主要 ...

最新文章

  1. pandas 修改数据和数据类型
  2. 自建SE16N功能,修改数据库表数据
  3. 小熊电器、九阳、苏泊尔们的“颜价比”被外卖小哥“打回原形”
  4. 恭喜你!在25岁前看到了这篇最最靠谱的深度学习入门指南
  5. 背计算机专业英语词汇,计算机专业英语词汇1500词(五)
  6. 从SharePoint 2013迁移到SharePoint Online - 评估工具
  7. 七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
  8. 【lstm做文本分类保存】
  9. SQLHelper--四种方法完整版
  10. word提示“Word上次启动失败,安全模式可以帮助您解决问题”的解决办法
  11. 网络攻防|XSS Flash弹窗钓鱼
  12. 初学者之路100个视频教程
  13. 【NOIP2017】滚粗记
  14. 浪潮PM8222-SHBA、RAID 2GB PM8204、RAID 4GB PM8204,阵列卡配置方法
  15. 关于动车:动车票假如象飞机票那样卖会如何?
  16. 买域名+配置SSL站点
  17. 计算机组成原理实训重要吗,计算机组成原理实训_报告.doc
  18. 茶云个人导航系统v1.2源码 带后台+网易云歌单播放功能+腾讯智能在线客服功能
  19. 【软件之道】Word模板的制作及使用
  20. NDIS和Rndis区别

热门文章

  1. 非常简单,让log4j输出mybatis的sql语句和执行结果
  2. seaJS 模块加载过程分析
  3. win7安装证书时无响应的解决办法
  4. [转载] 为什么this()和super()必须是构造函数中的第一条语句?
  5. launch edge 和 latch edge 延迟以及静态时序分析相关概念
  6. 开启和关闭oracle数据库中的审计功能
  7. 云计算一周动态2016-07-11
  8. iOS开发日记9-终端命令
  9. 给Fedora11安装五笔
  10. 【OpenCV】基本数据类型